Skip to content

Commit

Permalink
Fix bug when orientation changes
Browse files Browse the repository at this point in the history
  • Loading branch information
CaliCastle committed Sep 3, 2018
1 parent 17b5eb5 commit a6fb178
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 8 deletions.
38 changes: 31 additions & 7 deletions Example/Example/Storyboards/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
<color key="separatorColor" red="0.91400301399999995" green="0.91400301399999995" blue="0.91400301399999995" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
<imageView key="tableFooterView" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="PopMenu_Logo" id="ezY-er-s3g">
<rect key="frame" x="0.0" y="1808.3333333333333" width="414" height="73"/>
<rect key="frame" x="0.0" y="1874.3333333333333" width="414" height="73"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
</imageView>
<sections>
Expand Down Expand Up @@ -311,21 +311,21 @@
</tableViewCellContentView>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" accessoryType="disclosureIndicator" indentationWidth="10" textLabel="5Mf-rB-4Wn" detailTextLabel="g1e-Oi-WJT" rowHeight="66" style="IBUITableViewCellStyleSubtitle" id="iNk-6z-nUl">
<rect key="frame" x="0.0" y="963.66666666666663" width="414" height="65.999999999999886"/>
<rect key="frame" x="0.0" y="963.66666666666663" width="414" height="66"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="iNk-6z-nUl" id="MTH-kR-MZJ">
<rect key="frame" x="0.0" y="0.0" width="376" height="65.666666666666671"/>
<rect key="frame" x="0.0" y="0.0" width="388" height="65.666666666666671"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" insetsLayoutMarginsFromSafeArea="NO" text="Menu Corner Radius" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="5Mf-rB-4Wn">
<rect key="frame" x="20" y="10.333333333333336" width="151" height="22"/>
<rect key="frame" x="15" y="10.333333333333336" width="151" height="22"/>
<autoresizingMask key="autoresizingMask"/>
<fontDescription key="fontDescription" name="AvenirNext-DemiBold" family="Avenir Next" pointSize="16"/>
<color key="textColor" red="0.43137254899999999" green="0.64705882349999999" blue="0.45098039220000002" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" insetsLayoutMarginsFromSafeArea="NO" text="No radius" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="g1e-Oi-WJT">
<rect key="frame" x="20" y="35" width="58" height="18"/>
<rect key="frame" x="15" y="35" width="58" height="18"/>
<autoresizingMask key="autoresizingMask"/>
<fontDescription key="fontDescription" name="AvenirNext-Medium" family="Avenir Next" pointSize="13"/>
<color key="textColor" white="0.66666666669999997" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
Expand Down Expand Up @@ -550,12 +550,36 @@
</subviews>
</tableViewCellContentView>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" accessoryType="disclosureIndicator" indentationWidth="10" textLabel="fWt-48-Tuu" detailTextLabel="HgE-IA-0rO" rowHeight="66" style="IBUITableViewCellStyleSubtitle" id="siL-5h-9nO">
<rect key="frame" x="0.0" y="1623.6666666666665" width="414" height="66"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="siL-5h-9nO" id="VKL-vd-UC9">
<rect key="frame" x="0.0" y="0.0" width="376" height="65.666666666666671"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" insetsLayoutMarginsFromSafeArea="NO" text="Scrollable Menu" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="fWt-48-Tuu">
<rect key="frame" x="20" y="10.333333333333336" width="120" height="22"/>
<autoresizingMask key="autoresizingMask"/>
<fontDescription key="fontDescription" name="AvenirNext-DemiBold" family="Avenir Next" pointSize="16"/>
<color key="textColor" red="0.43137254899999999" green="0.64705882349999999" blue="0.45098039220000002" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" multipleTouchEnabled="YES" contentMode="left" insetsLayoutMarginsFromSafeArea="NO" text="When actions are more than 8" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" id="HgE-IA-0rO">
<rect key="frame" x="20" y="35" width="178.66666666666666" height="18"/>
<autoresizingMask key="autoresizingMask"/>
<fontDescription key="fontDescription" name="AvenirNext-Medium" family="Avenir Next" pointSize="13"/>
<color key="textColor" white="0.66666666669999997" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
</subviews>
</tableViewCellContentView>
</tableViewCell>
</cells>
</tableViewSection>
<tableViewSection id="oew-WO-ueB">
<cells>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" accessoryType="disclosureIndicator" indentationWidth="10" textLabel="f0a-hv-Y34" detailTextLabel="g8m-Yd-DK5" rowHeight="66" style="IBUITableViewCellStyleSubtitle" id="Tl1-km-tRb">
<rect key="frame" x="0.0" y="1659" width="414" height="66"/>
<rect key="frame" x="0.0" y="1725" width="414" height="66"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="Tl1-km-tRb" id="mA0-6F-Msb">
<rect key="frame" x="0.0" y="0.0" width="376" height="65.666666666666671"/>
Expand All @@ -579,7 +603,7 @@
</tableViewCellContentView>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" accessoryType="disclosureIndicator" indentationWidth="10" textLabel="2zw-a4-JPw" detailTextLabel="pLN-eg-zgf" rowHeight="66" style="IBUITableViewCellStyleSubtitle" id="0LF-vd-qKf">
<rect key="frame" x="0.0" y="1725" width="414" height="66"/>
<rect key="frame" x="0.0" y="1791" width="414" height="66"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="0LF-vd-qKf" id="tVL-px-I4u">
<rect key="frame" x="0.0" y="0.0" width="388" height="65.666666666666671"/>
Expand Down
22 changes: 22 additions & 0 deletions Example/Example/View Controllers/Helper/PopMenuExamples.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,5 +181,27 @@ final class PopMenuExamples {

return popMenu
}

/// Scrollable PopMenu when there's too many actions.
func popMenuScrollable() -> PopMenuViewController {
let actions = [
PopMenuDefaultAction(title: "Save to List", image: #imageLiteral(resourceName: "Plus"), color: #colorLiteral(red: 0.9764705896, green: 0.850980401, blue: 0.5490196347, alpha: 1)),
PopMenuDefaultAction(title: "Favorite", image: #imageLiteral(resourceName: "Heart"), color: #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)),
PopMenuDefaultAction(title: "Add to Cart", image: #imageLiteral(resourceName: "Cart_Add"), color: #colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1)),
PopMenuDefaultAction(title: "Save to List", image: #imageLiteral(resourceName: "Plus"), color: #colorLiteral(red: 0.9764705896, green: 0.850980401, blue: 0.5490196347, alpha: 1)),
PopMenuDefaultAction(title: "Favorite", image: #imageLiteral(resourceName: "Heart"), color: #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)),
PopMenuDefaultAction(title: "Add to Cart", image: #imageLiteral(resourceName: "Cart_Add"), color: #colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1)),
PopMenuDefaultAction(title: "Save to List", image: #imageLiteral(resourceName: "Plus"), color: #colorLiteral(red: 0.9764705896, green: 0.850980401, blue: 0.5490196347, alpha: 1)),
PopMenuDefaultAction(title: "Favorite", image: #imageLiteral(resourceName: "Heart"), color: #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)),
PopMenuDefaultAction(title: "Add to Cart", image: #imageLiteral(resourceName: "Cart_Add"), color: #colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1)),
PopMenuDefaultAction(title: "Save to List", image: #imageLiteral(resourceName: "Plus"), color: #colorLiteral(red: 0.9764705896, green: 0.850980401, blue: 0.5490196347, alpha: 1)),
PopMenuDefaultAction(title: "Favorite", image: #imageLiteral(resourceName: "Heart"), color: #colorLiteral(red: 0.4745098054, green: 0.8392156959, blue: 0.9764705896, alpha: 1)),
PopMenuDefaultAction(title: "Add to Cart", image: #imageLiteral(resourceName: "Cart_Add"), color: #colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1))
]

let popMenu = PopMenuViewController(actions: actions)

return popMenu
}

}
2 changes: 2 additions & 0 deletions Example/Example/View Controllers/RootViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ extension RootViewController {
popMenu = examples.popMenuCustomRenderingMode()
case (2, 13):
popMenu = examples.popMenuCustomSize()
case (2, 14):
popMenu = examples.popMenuScrollable()
case (3, 0):
let cell = tableView.cellForRow(at: indexPath)!
popMenu = examples.popMenuStandard(cell)
Expand Down
3 changes: 3 additions & 0 deletions PopMenu/Classes/PopMenuAppearance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ final public class PopMenuAppearance: NSObject {
/// How tall each action is.
public var popMenuActionHeight: CGFloat = 50

/// How many actions are the breakpoint to trigger scrollable.
public var popMenuActionCountForScrollable: UInt = 6

/// The separator style for each action.
public var popMenuItemSeparator: PopMenuActionSeparator = .none()

Expand Down
26 changes: 25 additions & 1 deletion PopMenu/View Controller & Views/PopMenuViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,21 @@ final public class PopMenuViewController: UIViewController {
return .lightContent
}

/// Handle when device orientation changed or container size changed.
///
/// - Parameters:
/// - size: Changed size
/// - coordinator: Coordinator that manages the container
public override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animate(alongsideTransition: { context in
self.configureBackgroundView()
self.contentFrame = self.calculateContentFittingFrame()
self.setupContentConstraints()
})

super.viewWillTransition(to: size, with: coordinator)
}

}

// MARK: - View Configurations
Expand Down Expand Up @@ -306,7 +321,16 @@ extension PopMenuViewController {
///
/// - Returns: The fitting frame
fileprivate func calculateContentFittingFrame() -> CGRect {
let size = CGSize(width: calculateContentWidth(), height: CGFloat(actions.count) * appearance.popMenuActionHeight)
var height: CGFloat

if actions.count >= 6 {
// Make scroll view
height = CGFloat(appearance.popMenuActionCountForScrollable) * appearance.popMenuActionHeight
} else {
height = CGFloat(actions.count) * appearance.popMenuActionHeight
}

let size = CGSize(width: calculateContentWidth(), height: height)
let origin = calculateContentOrigin(with: size)

return CGRect(origin: origin, size: size)
Expand Down

0 comments on commit a6fb178

Please sign in to comment.