Skip to content

Commit

Permalink
add top offset for navigationBar
Browse files Browse the repository at this point in the history
  • Loading branch information
Suric zhang committed Nov 3, 2016
1 parent 8bc74df commit e35aea3
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@
moduleName = "DropdownMenu"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/zhangxiaolian/Work/Suric-Open-Source/DropdownMenu/DropdownMenu/DropUpMenu.swift"
timestampString = "496209692.50319"
timestampString = "499850799.731409"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "172"
endingLineNumber = "172"
offsetFromSymbolStart = "28">
offsetFromSymbolStart = "26">
</Location>
<Location
shouldBeEnabled = "Yes"
Expand All @@ -83,12 +83,12 @@
moduleName = "DropdownMenu"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/zhangxiaolian/Work/Suric-Open-Source/DropdownMenu/DropdownMenu/DropUpMenu.swift"
timestampString = "496209692.504529"
timestampString = "499850799.73326"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "173"
endingLineNumber = "173"
offsetFromSymbolStart = "16">
offsetFromSymbolStart = "15">
</Location>
</Locations>
</BreakpointContent>
Expand Down
27 changes: 17 additions & 10 deletions DropdownMenu/DropdownMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ open class DropdownMenu: UIView {
fileprivate var windowRootView: UIView?
fileprivate var topConstraint: NSLayoutConstraint?
fileprivate var navigationBarCoverViewHeightConstraint: NSLayoutConstraint?
fileprivate let portraitTopOffset: CGFloat = 64.0
fileprivate let landscapeTopOffset: CGFloat = 32.0
fileprivate var topLayoutConstraintConstant: CGFloat {
return navigationController.navigationBar.frame.height + navigationController.navigationBar.frame.origin.y + topOffsetY
}

open weak var delegate: DropdownMenuDelegate?
open var animateDuration: TimeInterval = 0.25
Expand All @@ -40,13 +45,15 @@ open class DropdownMenu: UIView {
open var sectionHeaderHeight: CGFloat = 44
open var tableViewHeight: CGFloat = 0
open var defaultBottonMargin: CGFloat = 150
open var topOffsetY: CGFloat = 0

open var textColor: UIColor = UIColor(red: 56.0/255.0, green: 56.0/255.0, blue: 56.0/255.0, alpha: 1.0)
open var highlightColor: UIColor = UIColor(red: 3.0/255.0, green: 169.0/255.0, blue: 244.0/255.0, alpha: 1.0)
open var tableViewBackgroundColor: UIColor = UIColor(red: 242.0/255.0, green: 242.0/255.0, blue: 242.0/255.0, alpha: 1.0)
open var tableViewSeperatorColor = UIColor(red: 217.0/255.0, green: 217.0/255.0, blue: 217.0/255.0, alpha: 1.0)

open var displaySelected: Bool = true
open var displaySectionHeader: Bool = false

// section header sytle
open var sectionHeaderStyle: SectionHeaderStyle = SectionHeaderStyle()

Expand Down Expand Up @@ -90,17 +97,19 @@ open class DropdownMenu: UIView {
}

func updateForOrientationChange(_ nofication: Notification) {
print("UIApplicationWillChangeStatusBarOrientation")
if let oriention = (nofication as NSNotification).userInfo?[UIApplicationStatusBarOrientationUserInfoKey] as? Int {
var topOffset: CGFloat = 64.0
var topOffset: CGFloat
switch oriention {
case UIInterfaceOrientation.landscapeLeft.rawValue, UIInterfaceOrientation.landscapeRight.rawValue:
topOffset = 44.0
topOffset = landscapeTopOffset
default:
topOffset = 64.0
topOffset = portraitTopOffset
}
topOffset = topOffset + topOffsetY
topConstraint?.constant = topOffset
navigationBarCoverViewHeightConstraint?.constant = topOffset
UIView.animate(withDuration: 0.2, animations: {
UIView.animate(withDuration: 0.1, animations: {
self.windowRootView?.layoutIfNeeded()
})
}
Expand Down Expand Up @@ -132,8 +141,7 @@ open class DropdownMenu: UIView {

fileprivate func setupTableView() {
tableViewHeight = tableviewHeight()
let navigationBarFrame: CGRect = navigationController.navigationBar.frame
let maxHeight = navigationController.view.frame.height - navigationBarFrame.height + navigationBarFrame.origin.y - defaultBottonMargin
let maxHeight = navigationController.view.frame.height - topLayoutConstraintConstant - defaultBottonMargin
if tableViewHeight > maxHeight {
tableViewHeight = maxHeight
}
Expand All @@ -157,9 +165,8 @@ open class DropdownMenu: UIView {
view.addSubview(barCoverView)
barCoverView.translatesAutoresizingMaskIntoConstraints = false

let navigationBar = navigationController.navigationBar
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: barCoverView, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1.0, constant: 0)])
navigationBarCoverViewHeightConstraint = NSLayoutConstraint.init(item: barCoverView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: navigationBar.frame.height + navigationBar.frame.origin.y)
navigationBarCoverViewHeightConstraint = NSLayoutConstraint.init(item: barCoverView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: topLayoutConstraintConstant)
NSLayoutConstraint.activate([navigationBarCoverViewHeightConstraint!])
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: barCoverView, attribute: .left, relatedBy: .equal, toItem: view, attribute: .left, multiplier: 1.0, constant: 0)])
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: barCoverView, attribute: .right, relatedBy: .equal, toItem: view, attribute: .right, multiplier: 1.0, constant: 0)])
Expand Down Expand Up @@ -198,7 +205,7 @@ open class DropdownMenu: UIView {
windowRootView?.addSubview(self)

translatesAutoresizingMaskIntoConstraints = false
topConstraint = NSLayoutConstraint.init(item: self, attribute: .top, relatedBy: .equal, toItem: windowRootView, attribute: .top, multiplier: 1.0, constant: navigationController.navigationBar.frame.height + navigationController.navigationBar.frame.origin.y)
topConstraint = NSLayoutConstraint.init(item: self, attribute: .top, relatedBy: .equal, toItem: windowRootView, attribute: .top, multiplier: 1.0, constant: topLayoutConstraintConstant)
NSLayoutConstraint.activate([topConstraint!])
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: self, attribute: .bottom, relatedBy: .equal, toItem: windowRootView, attribute: .bottom, multiplier: 1.0, constant: 0)])
NSLayoutConstraint.activate([NSLayoutConstraint.init(item: self, attribute: .left, relatedBy: .equal, toItem: windowRootView, attribute: .left, multiplier: 1.0, constant: 0)])
Expand Down
56 changes: 49 additions & 7 deletions DropdownMenuDemo/DropdownMenuDemo/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11201" systemVersion="16A323" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="bEP-BO-oRt">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11542" systemVersion="16B2555" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="bEP-BO-oRt">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11161"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11524"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand All @@ -19,19 +23,55 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="have sections" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="BzP-Ey-vYz">
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<rect key="frame" x="138" y="160" width="100" height="20"/>
<fontDescription key="fontDescription" type="system" pointSize="16"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
</label>
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" on="YES" translatesAutoresizingMaskIntoConstraints="NO" id="25g-q9-wVt">
<rect key="frame" x="163" y="188" width="51" height="31"/>
<connections>
<action selector="sectionSwitchValueChanged:" destination="BYZ-38-t0r" eventType="valueChanged" id="yt0-i8-kxP"/>
</connections>
</switch>
<slider opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" minValue="0.0" maxValue="100" translatesAutoresizingMaskIntoConstraints="NO" id="PVH-7j-Yyz">
<rect key="frame" x="44" y="120" width="287" height="31"/>
<connections>
<action selector="changeOffsetAction:" destination="BYZ-38-t0r" eventType="valueChanged" id="Qpj-wW-J0V"/>
</connections>
</slider>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="DropDownMenu Top Offset: 0" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Ymy-Wm-YFe">
<rect key="frame" x="85.5" y="82" width="204.5" height="18"/>
<fontDescription key="fontDescription" type="system" pointSize="15"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="0" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="nuX-bM-BJi">
<rect key="frame" x="27.5" y="125" width="10.5" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="100" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="UPa-at-gEy">
<rect key="frame" x="338" y="125" width="29" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="BzP-Ey-vYz" firstAttribute="centerY" secondItem="8bC-Xf-vdC" secondAttribute="centerY" id="Q4n-FJ-PHj"/>
<constraint firstItem="BzP-Ey-vYz" firstAttribute="top" secondItem="PVH-7j-Yyz" secondAttribute="bottom" constant="10" id="02R-Sd-K1e"/>
<constraint firstItem="PVH-7j-Yyz" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leadingMargin" constant="30" id="0Lu-c0-q3U"/>
<constraint firstItem="Ymy-Wm-YFe" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="18" id="45G-S5-ZiT"/>
<constraint firstAttribute="trailingMargin" secondItem="PVH-7j-Yyz" secondAttribute="trailing" constant="30" id="4t7-hS-eoX"/>
<constraint firstAttribute="trailing" secondItem="UPa-at-gEy" secondAttribute="trailing" constant="8" id="7wh-Fk-E7w"/>
<constraint firstItem="PVH-7j-Yyz" firstAttribute="leading" secondItem="nuX-bM-BJi" secondAttribute="trailing" constant="8" id="Oi0-1S-YG7"/>
<constraint firstItem="PVH-7j-Yyz" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="Q1c-nW-fIa"/>
<constraint firstItem="nuX-bM-BJi" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="61" id="Yfw-xR-7zO"/>
<constraint firstItem="UPa-at-gEy" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" constant="61" id="Zxl-Jb-fWH"/>
<constraint firstItem="PVH-7j-Yyz" firstAttribute="top" secondItem="Ymy-Wm-YFe" secondAttribute="bottom" constant="20" id="cN4-3J-XBo"/>
<constraint firstItem="Ymy-Wm-YFe" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="n3a-uW-YJF"/>
<constraint firstItem="25g-q9-wVt" firstAttribute="centerX" secondItem="BzP-Ey-vYz" secondAttribute="centerX" id="uiI-Kb-AoW"/>
<constraint firstItem="25g-q9-wVt" firstAttribute="top" secondItem="BzP-Ey-vYz" secondAttribute="bottom" constant="8" id="y3J-U1-nOz"/>
<constraint firstItem="BzP-Ey-vYz" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="zco-dm-bDh"/>
Expand All @@ -50,12 +90,14 @@
</barButtonItem>
</navigationItem>
<connections>
<outlet property="offSetSlider" destination="PVH-7j-Yyz" id="LHr-yl-4oP"/>
<outlet property="offsetLabel" destination="Ymy-Wm-YFe" id="quD-HW-W9d"/>
<outlet property="sectionSwitch" destination="25g-q9-wVt" id="wk0-sS-ZvT"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1831" y="349"/>
<point key="canvasLocation" x="1829.5999999999999" y="348.57571214392806"/>
</scene>
<!--Navigation Controller-->
<scene sceneID="4ex-XS-o8X">
Expand Down Expand Up @@ -86,10 +128,10 @@
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="default" indentationWidth="10" reuseIdentifier="CustomCell" id="0qN-ja-FGY">
<rect key="frame" x="0.0" y="92" width="375" height="44"/>
<rect key="frame" x="0.0" y="28" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="0qN-ja-FGY" id="TCk-ux-5Dk">
<frame key="frameInset" width="375" height="43.5"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="43"/>
<autoresizingMask key="autoresizingMask"/>
</tableViewCellContentView>
</tableViewCell>
Expand Down
8 changes: 8 additions & 0 deletions DropdownMenuDemo/DropdownMenuDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import DropdownMenu

class ViewController: UIViewController {
@IBOutlet weak var sectionSwitch: UISwitch!
@IBOutlet weak var offSetSlider: UISlider!
@IBOutlet weak var offsetLabel: UILabel!

var showSection: Bool = true
var selectedRow: Int = 0
Expand All @@ -26,6 +28,11 @@ class ViewController: UIViewController {
// Dispose of any resources that can be recreated.
}

@IBAction func changeOffsetAction(_ sender: UISlider) {
offsetLabel.text = "DropDownMenu Top Offset: \(sender.value)"
}


@IBAction func showMenu(_ sender: UIBarButtonItem) {
let item1 = DropdownItem(title: "NO Image")
let item2 = DropdownItem(image: UIImage(named: "file")!, title: "File")
Expand All @@ -43,6 +50,7 @@ class ViewController: UIViewController {
items = [[item1, item2, item3, item4]]
menuView = DropdownMenu(navigationController: navigationController!, items: [item1, item2, item3, item4], selectedRow: selectedRow)
}
menuView?.topOffsetY = CGFloat(offSetSlider.value)
menuView?.delegate = self
menuView?.showMenu()
}
Expand Down

0 comments on commit e35aea3

Please sign in to comment.