Skip to content

Commit

Permalink
Merge pull request #21 from CaliCastle/develop
Browse files Browse the repository at this point in the history
Fix #19 and prevent menu offscreen
  • Loading branch information
CaliCastle authored Jan 5, 2019
2 parents 4e81d43 + 2d7a7bd commit 90c75c0
Show file tree
Hide file tree
Showing 8 changed files with 306 additions and 115 deletions.
105 changes: 105 additions & 0 deletions Example/Example.xcodeproj/xcshareddata/xcschemes/Example.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1010"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9D397AB520805CE400C58037"
BuildableName = "Example.app"
BlueprintName = "Example"
ReferencedContainer = "container:Example.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "NO"
buildForRunning = "NO"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9D397A9020802C1600C58037"
BuildableName = "PopMenu.framework"
BlueprintName = "PopMenu"
ReferencedContainer = "container:../PopMenu.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9D397AB520805CE400C58037"
BuildableName = "Example.app"
BlueprintName = "Example"
ReferencedContainer = "container:Example.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9D397AB520805CE400C58037"
BuildableName = "Example.app"
BlueprintName = "Example"
ReferencedContainer = "container:Example.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "9D397AB520805CE400C58037"
BuildableName = "Example.app"
BlueprintName = "Example"
ReferencedContainer = "container:Example.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
204 changes: 105 additions & 99 deletions Example/Example/Storyboards/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ final class PopMenuExamples {
PopMenuDefaultAction(title: "Short"),
PopMenuDefaultAction(title: "A Longer Text")
]

let popMenu = PopMenuViewController(actions: actions)

// TODO: Not ready yet
// popMenu.appearance.popMenuPresentationStyle = .near(.bottom)

return popMenu
}

Expand Down
40 changes: 37 additions & 3 deletions Example/Example/View Controllers/RootViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,32 @@ class RootViewController: UITableViewController {
// PopMenu examples helper instance.
fileprivate let examples = PopMenuExamples()

// Whether or not should use manager to present menu
fileprivate let shouldUseManager = true

// MARK: - View Life Cycle

override func viewDidLoad() {
super.viewDidLoad()

}

/// Top right bar button tapped.
/// Top left bar button tapped
@IBAction func topLeftButtonDidTap(_ sender: UIBarButtonItem) {
if shouldUseManager {
showMenuWithManager(for: sender)
} else {
showMenuManually(for: sender)
}
}

@IBAction func presentMenu(_ sender: UIBarButtonItem) {
showMenuManually(for: sender)
/// Top right bar button tapped
@IBAction func topRightButtonDidTap(_ sender: UIBarButtonItem) {
if shouldUseManager {
showMenuWithManager(for: sender)
} else {
showMenuManually(for: sender)
}
}

/// This shows how to use PopMenu the old fashion way
Expand Down Expand Up @@ -61,6 +76,25 @@ class RootViewController: UITableViewController {
present(controller, animated: true, completion: nil)
}

/// This shows how to use PopMenu with PopMenuManager
fileprivate func showMenuWithManager(for barButtonItem: UIBarButtonItem) {
// Get manager instance
let manager = PopMenuManager.default
// Set actions
manager.actions = [
PopMenuDefaultAction(title: "Click me to", image: #imageLiteral(resourceName: "Plus"), color: .yellow),
PopMenuDefaultAction(title: "Pop another menu", image: #imageLiteral(resourceName: "Heart"), color: #colorLiteral(red: 0.9816910625, green: 0.5655395389, blue: 0.4352460504, alpha: 1)),
PopMenuDefaultAction(title: "Try it out!", image: nil, color: .white)
]
// Customize appearance
manager.popMenuAppearance.popMenuFont = UIFont(name: "AvenirNext-DemiBold", size: 16)!
manager.popMenuAppearance.popMenuBackgroundStyle = .blurred(.dark)
manager.popMenuShouldDismissOnSelection = false
manager.popMenuDelegate = self
// Present menu
manager.present(sourceView: barButtonItem)
}

// MARK: - Table View Row Configuration

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
Expand Down
6 changes: 3 additions & 3 deletions NewPopMenu.podspec
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Pod::Spec.new do |spec|
spec.name = 'NewPopMenu'
spec.version = '2.1.1'
spec.version = '2.1.2'
spec.license = { :type => 'MIT', :file => "LICENSE" }
spec.homepage = 'https://github.com/CaliCastle/PopMenu'
spec.authors = { 'Cali Castle' => '[email protected]' }
spec.summary = 'A cool and customizable popup action sheet for iOS'
spec.source = { :git => 'https://github.com/CaliCastle/PopMenu.git', :tag => 'v2.1.1' }
spec.summary = 'A fully customizable popup style menu'
spec.source = { :git => 'https://github.com/CaliCastle/PopMenu.git', :tag => 'v2.1.2' }
spec.source_files = 'PopMenu/**/*.{h,swift}'

spec.module_name = "PopMenu"
Expand Down
31 changes: 31 additions & 0 deletions PopMenu/Classes/PopMenuAppearance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ final public class PopMenuAppearance: NSObject {

/// The status bar style of the pop menu.
public var popMenuStatusBarStyle: UIStatusBarStyle?

/// The presentation style
public var popMenuPresentationStyle: PopMenuPresentationStyle = .cover()

}

Expand Down Expand Up @@ -168,3 +171,31 @@ public struct PopMenuActionSeparator: Equatable {
}

}

///
public struct PopMenuPresentationStyle {

/// The direction enum for the menu.
public let direction: PopMenuDirection

/// Custom offset coordinates.
public let offset: CGPoint?

/// The default presentation that covers the source view.
public static func cover() -> PopMenuPresentationStyle {
return PopMenuPresentationStyle(direction: .none, offset: nil)
}

/// The custom presentation that shows near the source view in a direction and offset.
public static func near(_ direction: PopMenuDirection, offset: CGPoint? = nil) -> PopMenuPresentationStyle {
return PopMenuPresentationStyle(direction: direction, offset: offset)
}
}

public enum PopMenuDirection {
case top
case left
case right
case bottom
case none
}
12 changes: 5 additions & 7 deletions PopMenu/View Controller & Views/PopMenuAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,11 @@ public class PopMenuDefaultAction: NSObject, PopMenuAction {
UIView.animate(withDuration: 0.175, animations: {
self.view.transform = CGAffineTransform.identity.scaledBy(x: 0.915, y: 0.915)
self.view.backgroundColor = self.backgroundColor.withAlphaComponent(0.18)
}, completion: {
if $0 {
UIView.animate(withDuration: 0.175, animations: {
self.view.transform = .identity
self.view.backgroundColor = .clear
})
}
}, completion: { _ in
UIView.animate(withDuration: 0.175, animations: {
self.view.transform = .identity
self.view.backgroundColor = .clear
})
})
}
}
Expand Down
18 changes: 16 additions & 2 deletions PopMenu/View Controller & Views/PopMenuViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ final public class PopMenuViewController: UIViewController {

// MARK: - View Life Cycle

/// PopMenuViewController constructor
///
/// - Parameters:
/// - sourceView: the source view for triggering the menu
/// - actions: all the menu actions
/// - appearance: appearance configuration
public convenience init(sourceView: AnyObject? = nil, actions: [PopMenuAction], appearance: PopMenuAppearance? = nil) {
self.init(nibName: nil, bundle: nil)

Expand Down Expand Up @@ -342,10 +348,18 @@ extension PopMenuViewController {
/// - Returns: The source origin point
fileprivate func calculateContentOrigin(with size: CGSize) -> CGPoint {
guard let sourceFrame = absoluteSourceFrame else { return CGPoint(x: view.center.x - size.width / 2, y: view.center.y - size.height / 2) }
let minContentPos: CGFloat = UIScreen.main.bounds.size.width * 0.05
let maxContentPos: CGFloat = UIScreen.main.bounds.size.width * 0.95

// Get desired content origin point
let offsetX = (size.width - sourceFrame.size.width ) / 2
var desiredOrigin = CGPoint(x: sourceFrame.origin.x - offsetX, y: sourceFrame.origin.y)
if (desiredOrigin.x + size.width) > maxContentPos {
desiredOrigin.x = maxContentPos - size.width
}
if desiredOrigin.x < minContentPos {
desiredOrigin.x = minContentPos
}

// Move content in place
translateOverflowX(desiredOrigin: &desiredOrigin, contentSize: size)
Expand Down Expand Up @@ -415,12 +429,12 @@ extension PopMenuViewController {
sizingLabel.text = action.title

let desiredWidth = sizingLabel.sizeThatFits(view.bounds.size).width
contentFitWidth += min(desiredWidth, maxContentWidth)
contentFitWidth += desiredWidth

contentFitWidth += action.iconWidthHeight
}

return contentFitWidth
return min(contentFitWidth,maxContentWidth)
}

/// Setup actions view.
Expand Down

0 comments on commit 90c75c0

Please sign in to comment.