Skip to content

Commit

Permalink
Fix issue "Appear overtop tab bar controller"
Browse files Browse the repository at this point in the history
  • Loading branch information
tuanphung committed Aug 4, 2015
1 parent d3e4bdc commit dc12474
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 25 deletions.
58 changes: 53 additions & 5 deletions Demo/Demo/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7706" systemVersion="14E46" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="5nB-CE-XEl">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7706" systemVersion="14D131" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="s8W-fO-vqO">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7703"/>
</dependencies>
Expand All @@ -17,10 +17,19 @@
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" alpha="0.69999999999999996" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="1" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Jc6-f8-1fo" userLabel="Label">
<rect key="frame" x="286.5" y="266.5" width="28" height="68.5"/>
<rect key="frame" x="286" y="266" width="28" height="69"/>
<constraints>
<constraint firstAttribute="height" constant="69" id="7JE-JN-zWe"/>
<constraint firstAttribute="width" constant="28" id="Mxu-zV-LiU"/>
</constraints>
<fontDescription key="fontDescription" name="Avenir-Roman" family="Avenir" pointSize="50"/>
<color key="textColor" white="0.33333333333333331" alpha="1" colorSpace="calibratedWhite"/>
<nil key="highlightedColor"/>
<variation key="default">
<mask key="constraints">
<exclude reference="Mxu-zV-LiU"/>
</mask>
</variation>
</label>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="0.82999999999999996" colorSpace="calibratedRGB"/>
Expand All @@ -36,12 +45,13 @@
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1090" y="246"/>
<point key="canvasLocation" x="1433" y="356"/>
</scene>
<!--Navigation Controller-->
<!--Item-->
<scene sceneID="GTl-3t-6dt">
<objects>
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="5nB-CE-XEl" sceneMemberID="viewController">
<tabBarItem key="tabBarItem" title="Item" id="60w-9J-6f0"/>
<toolbarItems/>
<navigationBar key="navigationBar" contentMode="scaleToFill" id="Yvl-Sk-d0l">
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
Expand All @@ -54,7 +64,45 @@
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="S9P-Dt-6Jc" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="360" y="246"/>
<point key="canvasLocation" x="720" y="356"/>
</scene>
<!--Item-->
<scene sceneID="vzF-qC-fr3">
<objects>
<viewController id="bW5-PG-JQh" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="sfp-WO-NhM"/>
<viewControllerLayoutGuide type="bottom" id="eQO-J5-ySf"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="nN6-1u-YNS">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
</view>
<tabBarItem key="tabBarItem" title="Item" id="BSz-Fb-oZd"/>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="Kz2-uR-app" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="720" y="1154"/>
</scene>
<!--Tab Bar Controller-->
<scene sceneID="Bh9-FN-aRD">
<objects>
<tabBarController id="s8W-fO-vqO" sceneMemberID="viewController">
<nil key="simulatedBottomBarMetrics"/>
<tabBar key="tabBar" contentMode="scaleToFill" id="iPK-ay-1kR">
<rect key="frame" x="0.0" y="0.0" width="320" height="49"/>
<autoresizingMask key="autoresizingMask"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
</tabBar>
<connections>
<segue destination="5nB-CE-XEl" kind="relationship" relationship="viewControllers" id="v5G-Sw-qfz"/>
<segue destination="bW5-PG-JQh" kind="relationship" relationship="viewControllers" id="aca-dG-kcc"/>
</connections>
</tabBarController>
<placeholder placeholderIdentifier="IBFirstResponder" id="uZe-OA-bX2" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="38" y="356"/>
</scene>
</scenes>
</document>
5 changes: 4 additions & 1 deletion Demo/Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ class ViewController: UIViewController {
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
UINavigationBar.appearance().barStyle = UIBarStyle.Default

let menuView = BTNavigationDropdownMenu(frame: CGRectMake(0.0, 0.0, 300, 44), title: items.first!, items: items, containerView: self.view)
let window = UIApplication.sharedApplication().delegate!.window!!
// let menuView = BTNavigationDropdownMenu(frame: CGRectMake(0.0, 0.0, 300, 44), title: items.first!, items: items, containerView: self.view)
let menuView = BTNavigationDropdownMenu(frame: CGRectMake(0.0, 0.0, 300, 44), title: items.first!, items: items, containerView: window, padding: UIEdgeInsets(top: 64, left: 0, bottom: 0, right: 0))

menuView.cellHeight = 50
menuView.cellBackgroundColor = self.navigationController?.navigationBar.barTintColor
menuView.cellSelectionColor = UIColor(red: 0.0/255.0, green:160.0/255.0, blue:195.0/255.0, alpha: 1.0)
Expand Down
53 changes: 34 additions & 19 deletions Source/BTNavigationDropdownMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,10 @@ public class BTNavigationDropdownMenu: UIView {
public var didSelectItemAtIndexHandler: ((indexPath: Int) -> ())?

// Private properties
private var tableContainerView: UIView!
// private var tableContainerView: UIView!
// private var mainScreenBounds: CGRect!

private var configuration: BTConfiguration!
private var mainScreenBounds: CGRect!
private var menuButton: UIButton!
private var menuTitle: UILabel!
private var menuArrow: UIImageView!
Expand All @@ -123,18 +124,18 @@ public class BTNavigationDropdownMenu: UIView {
private var isShown: Bool!
private var navigationBarHeight: CGFloat!

private var menuViewWrapper: UIView!

required public init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

public init(frame: CGRect, title: String, items: [AnyObject], containerView:UIView) {
public init(frame: CGRect, title: String, items: [AnyObject], containerView:UIView, padding: UIEdgeInsets = UIEdgeInsetsZero) {
super.init(frame:frame)

// Init properties
self.configuration = BTConfiguration()
self.tableContainerView = containerView
self.navigationBarHeight = 44
self.mainScreenBounds = UIScreen.mainScreen().bounds
self.isShown = false
self.items = items

Expand All @@ -153,15 +154,35 @@ public class BTNavigationDropdownMenu: UIView {
self.menuArrow = UIImageView(image: self.configuration.arrowImage)
self.menuButton.addSubview(self.menuArrow)

var containerBounds = containerView.bounds

// Set up DropdownMenu
self.menuViewWrapper = UIView(frame: CGRectMake(containerBounds.origin.x + padding.left, padding.top, containerBounds.width - (padding.left + padding.right), containerBounds.height - (padding.top + padding.bottom)))
self.menuViewWrapper.clipsToBounds = true

// Init background view (under table view)
self.backgroundView = UIView(frame: containerBounds)
self.backgroundView.backgroundColor = self.configuration.maskBackgroundColor

// Init table view
self.tableView = BTTableView(frame: CGRectMake(mainScreenBounds.origin.x, mainScreenBounds.origin.y, mainScreenBounds.width, mainScreenBounds.height + 300 - 64), items: items, configuration: self.configuration)
self.tableView = BTTableView(frame: CGRectMake(containerBounds.origin.x, containerBounds.origin.y, containerBounds.width, containerBounds.height + 300 - 64), items: items, configuration: self.configuration)
self.tableView.selectRowAtIndexPathHandler = { (indexPath: Int) -> () in
self.didSelectItemAtIndexHandler!(indexPath: indexPath)
self.setMenuTitle("\(items[indexPath])")
self.hideMenu()
self.isShown = false
self.layoutSubviews()
}

// Add background view & table view to container view
self.menuViewWrapper.addSubview(self.backgroundView)
self.menuViewWrapper.addSubview(self.tableView)

// Add Menu View to container view
containerView.addSubview(self.menuViewWrapper)

// By default, hide menu view
self.menuViewWrapper.hidden = true
}

override public func layoutSubviews() {
Expand All @@ -177,26 +198,21 @@ public class BTNavigationDropdownMenu: UIView {
headerView.backgroundColor = self.configuration.cellBackgroundColor
self.tableView.tableHeaderView = headerView

// Reload data to dismiss highlight color of selected cell
self.tableView.reloadData()

// Init background view (under table view)
self.backgroundView = UIView(frame: mainScreenBounds)
self.backgroundView.backgroundColor = self.configuration.maskBackgroundColor

// Add background view & table view to container view
self.tableContainerView.addSubview(self.backgroundView)
self.tableContainerView.addSubview(self.tableView)

// Rotate arrow
self.rotateArrow()

// Visible menu view
self.menuViewWrapper.hidden = false

// Change background alpha
self.backgroundView.alpha = 0

// Animation
self.tableView.frame.origin.y = -CGFloat(self.items.count) * self.configuration.cellHeight - 300

// Reload data to dismiss highlight color of selected cell
self.tableView.reloadData()

UIView.animateWithDuration(
self.configuration.animationDuration * 1.5,
delay: 0,
Expand Down Expand Up @@ -233,8 +249,7 @@ public class BTNavigationDropdownMenu: UIView {
self.tableView.frame.origin.y = -CGFloat(self.items.count) * self.configuration.cellHeight - 300
self.backgroundView.alpha = 0
}, completion: { _ in
self.tableView.removeFromSuperview()
self.backgroundView.removeFromSuperview()
self.menuViewWrapper.hidden = true
})
}

Expand Down

0 comments on commit dc12474

Please sign in to comment.