Commit 96148cc4 by Daniel Dahan

added NavigationBar example

parent f4ce0048
...@@ -47,6 +47,9 @@ ...@@ -47,6 +47,9 @@
location = "group:Programmatic/MenuViewController/MenuViewController.xcodeproj"> location = "group:Programmatic/MenuViewController/MenuViewController.xcodeproj">
</FileRef> </FileRef>
<FileRef <FileRef
location = "group:Programmatic/NavigationBar/NavigationBar.xcodeproj">
</FileRef>
<FileRef
location = "group:Programmatic/SearchBarView/SearchBarView.xcodeproj"> location = "group:Programmatic/SearchBarView/SearchBarView.xcodeproj">
</FileRef> </FileRef>
<FileRef <FileRef
......
...@@ -39,7 +39,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate { ...@@ -39,7 +39,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch. // Override point for customization after application launch.
window = UIWindow(frame: UIScreen.mainScreen().bounds) window = UIWindow(frame: UIScreen.mainScreen().bounds)
// window!.rootViewController = SideNavigationViewController(mainViewController: AppMenuViewController(mainViewController: AppNavigationBarViewController(mainViewController: InboxViewController())), leftViewController: AppLeftViewController(), rightViewController: AppRightViewController())
window!.rootViewController = SideNavigationViewController(mainViewController: AppMenuViewController(mainViewController: AppNavigationController(rootViewController: FeedViewController())), leftViewController: AppLeftViewController(), rightViewController: AppRightViewController()) window!.rootViewController = SideNavigationViewController(mainViewController: AppMenuViewController(mainViewController: AppNavigationController(rootViewController: FeedViewController())), leftViewController: AppLeftViewController(), rightViewController: AppRightViewController())
window!.makeKeyAndVisible() window!.makeKeyAndVisible()
return true return true
......
...@@ -32,27 +32,39 @@ import UIKit ...@@ -32,27 +32,39 @@ import UIKit
import Material import Material
class FeedViewController: UIViewController { class FeedViewController: UIViewController {
/// Menu button at the top left of the navigation bar. /// NavigationBar title label.
private lazy var menuButton: FlatButton = FlatButton() private var titleLabel: UILabel!
/// Search button at the top left of the navigation bar. /// NavigationBar menu button.
private lazy var searchButton: FlatButton = FlatButton() private var menuButton: FlatButton!
/// NavigationBar switch control.
private var switchControl: MaterialSwitch!
/// NavigationBar search button.
private var searchButton: FlatButton!
/// MaterialCollectionView. /// MaterialCollectionView.
private lazy var collectionView: MaterialCollectionView = MaterialCollectionView() private var collectionView: MaterialCollectionView!
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
prepareView() prepareView()
prepareTitleLabel()
prepareMenuButton() prepareMenuButton()
prepareSwitchControl()
prepareSearchButton() prepareSearchButton()
prepareNavigationBar()
prepareCollectionView() prepareCollectionView()
} }
override func viewWillAppear(animated: Bool) { override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated) super.viewWillAppear(animated)
// Ensure that the SideNavigation is enabled.
sideNavigationViewController?.enabled = true sideNavigationViewController?.enabled = true
// Ensure that the NavigationBar is styled correctly.
if let navigationbar: NavigationBar = navigationController?.navigationBar as? NavigationBar { if let navigationbar: NavigationBar = navigationController?.navigationBar as? NavigationBar {
navigationbar.statusBarStyle = .LightContent navigationbar.statusBarStyle = .LightContent
navigationbar.backgroundColor = MaterialColor.blue.base navigationbar.backgroundColor = MaterialColor.blue.base
...@@ -72,36 +84,32 @@ class FeedViewController: UIViewController { ...@@ -72,36 +84,32 @@ class FeedViewController: UIViewController {
/// Handler for searchButton. /// Handler for searchButton.
internal func handleSearchButton() { internal func handleSearchButton() {
let vc: AppSearchBarViewController = AppSearchBarViewController(mainViewController: SearchListViewController()) navigationController?.presentViewController(AppSearchBarViewController(mainViewController: SearchListViewController()), animated: true, completion: nil)
navigationController?.modalPresentationStyle = .Popover
navigationController?.presentViewController(vc, animated: true, completion: nil)
} }
/// Prepares view.
private func prepareView() { private func prepareView() {
view.backgroundColor = MaterialColor.grey.lighten4 view.backgroundColor = MaterialColor.grey.lighten4
}
let titleLabel: UILabel = UILabel()
titleLabel.text = "Material" /// Prepares view.
titleLabel.textAlignment = .Left private func prepareNavigationBar() {
titleLabel.textColor = MaterialColor.white
let detailLabel: UILabel = UILabel()
detailLabel.text = "Build Beautiful Software"
detailLabel.textAlignment = .Left
detailLabel.textColor = MaterialColor.white
navigationItem.titleLabel = titleLabel navigationItem.titleLabel = titleLabel
navigationItem.detailLabel = detailLabel
navigationItem.leftControls = [menuButton] navigationItem.leftControls = [menuButton]
navigationItem.rightControls = [searchButton] navigationItem.rightControls = [switchControl, searchButton]
}
/// Prepares the titleLabel.
private func prepareTitleLabel() {
titleLabel = UILabel()
titleLabel.text = "Inbox"
titleLabel.textAlignment = .Left
titleLabel.textColor = MaterialColor.white
} }
/// Prepares the menuButton. /// Prepares the menuButton.
private func prepareMenuButton() { private func prepareMenuButton() {
let image: UIImage? = UIImage(named: "ic_menu_white") let image: UIImage? = MaterialIcon.menu
menuButton = FlatButton()
menuButton.pulseScale = false menuButton.pulseScale = false
menuButton.pulseColor = MaterialColor.white menuButton.pulseColor = MaterialColor.white
menuButton.setImage(image, forState: .Normal) menuButton.setImage(image, forState: .Normal)
...@@ -109,10 +117,15 @@ class FeedViewController: UIViewController { ...@@ -109,10 +117,15 @@ class FeedViewController: UIViewController {
menuButton.addTarget(self, action: "handleMenuButton", forControlEvents: .TouchUpInside) menuButton.addTarget(self, action: "handleMenuButton", forControlEvents: .TouchUpInside)
} }
/// Prepares the switchControl.
private func prepareSwitchControl() {
switchControl = MaterialSwitch(state: .Off, style: .LightContent, size: .Small)
}
/// Prepares the searchButton. /// Prepares the searchButton.
private func prepareSearchButton() { private func prepareSearchButton() {
// Search button. let image: UIImage? = MaterialIcon.search
let image: UIImage? = UIImage(named: "ic_search_white") searchButton = FlatButton()
searchButton.pulseScale = false searchButton.pulseScale = false
searchButton.pulseColor = MaterialColor.white searchButton.pulseColor = MaterialColor.white
searchButton.setImage(image, forState: .Normal) searchButton.setImage(image, forState: .Normal)
...@@ -120,13 +133,15 @@ class FeedViewController: UIViewController { ...@@ -120,13 +133,15 @@ class FeedViewController: UIViewController {
searchButton.addTarget(self, action: "handleSearchButton", forControlEvents: .TouchUpInside) searchButton.addTarget(self, action: "handleSearchButton", forControlEvents: .TouchUpInside)
} }
/// Prepares the collectionView /// Prepares the collectionView.
private func prepareCollectionView() { private func prepareCollectionView() {
collectionView = MaterialCollectionView()
collectionView.dataSource = self collectionView.dataSource = self
collectionView.delegate = self collectionView.delegate = self
collectionView.spacingPreset = .Spacing1 collectionView.spacingPreset = .Spacing1
collectionView.registerClass(MaterialCollectionViewCell.self, forCellWithReuseIdentifier: "MaterialCollectionViewCell") collectionView.registerClass(MaterialCollectionViewCell.self, forCellWithReuseIdentifier: "MaterialCollectionViewCell")
// Layout the collectionView.
view.addSubview(collectionView) view.addSubview(collectionView)
collectionView.translatesAutoresizingMaskIntoConstraints = false collectionView.translatesAutoresizingMaskIntoConstraints = false
MaterialLayout.alignToParent(view, child: collectionView) MaterialLayout.alignToParent(view, child: collectionView)
...@@ -206,11 +221,11 @@ extension FeedViewController: MaterialCollectionViewDataSource { ...@@ -206,11 +221,11 @@ extension FeedViewController: MaterialCollectionViewDataSource {
if let data: Dictionary<String, AnyObject> = item.data as? Dictionary<String, AnyObject> { if let data: Dictionary<String, AnyObject> = item.data as? Dictionary<String, AnyObject> {
var cardView: CardView? = c.contentView.subviews.first as? CardView var cardView: ImageCardView? = c.contentView.subviews.first as? ImageCardView
// Only build the template if the CardView doesn't exist. // Only build the template if the CardView doesn't exist.
if nil == cardView { if nil == cardView {
cardView = CardView() cardView = ImageCardView()
c.backgroundColor = nil c.backgroundColor = nil
c.pulseColor = nil c.pulseColor = nil
......
...@@ -38,16 +38,24 @@ private struct Item { ...@@ -38,16 +38,24 @@ private struct Item {
} }
class InboxViewController: UIViewController { class InboxViewController: UIViewController {
/// NavigationBar title label.
private var titleLabel: UILabel!
/// NavigationBar detail label.
private var detailLabel: UILabel!
/// A tableView used to display Bond entries. /// A tableView used to display Bond entries.
private let tableView: UITableView = UITableView() private var tableView: UITableView!
/// A list of all the Author Bond types. /// A list of all the Author Bond types.
private var items: Array<Item> = Array<Item>() private var items: Array<Item>!
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
prepareItems() prepareItems()
prepareView() prepareTitleLabel()
prepareDetailLabel()
prepareNavigationBar()
prepareTableView() prepareTableView()
} }
...@@ -68,32 +76,19 @@ class InboxViewController: UIViewController { ...@@ -68,32 +76,19 @@ class InboxViewController: UIViewController {
private func prepareView() { private func prepareView() {
view.backgroundColor = MaterialColor.white view.backgroundColor = MaterialColor.white
let titleLabel: UILabel = UILabel()
titleLabel.text = "Inbox"
titleLabel.textAlignment = .Left
titleLabel.textColor = MaterialColor.white
let detailLabel: UILabel = UILabel()
detailLabel.text = "\(items.count) Contacts"
detailLabel.textAlignment = .Left
detailLabel.textColor = MaterialColor.white
let switchControl: MaterialSwitch = MaterialSwitch(state: .Off, style: .LightContent, size: .Small)
let image: UIImage? = UIImage(named: "ic_share_white")
let shareButton: FlatButton = FlatButton()
shareButton.pulseScale = false
shareButton.pulseColor = MaterialColor.white
shareButton.setImage(image, forState: .Normal)
shareButton.setImage(image, forState: .Highlighted)
navigationItem.titleLabel = titleLabel navigationItem.titleLabel = titleLabel
// navigationItem.detailLabel = detailLabel navigationItem.detailLabel = detailLabel
navigationItem.rightControls = [switchControl, shareButton] }
/// Prepares the NavigationBar.
private func prepareNavigationBar() {
navigationItem.titleLabel = titleLabel
navigationItem.detailLabel = detailLabel
} }
/// Prepares the items Array. /// Prepares the items Array.
private func prepareItems() { private func prepareItems() {
items = Array<Item>()
items.append(Item(text: "Summer BBQ", detail: "Wish I could come, but I am out of town this weekend.", image: UIImage(named: "Profile1"))) items.append(Item(text: "Summer BBQ", detail: "Wish I could come, but I am out of town this weekend.", image: UIImage(named: "Profile1")))
items.append(Item(text: "Birthday gift", detail: "Have any ideas about what we should get Heidi for her birthday?", image: UIImage(named: "Profile2"))) items.append(Item(text: "Birthday gift", detail: "Have any ideas about what we should get Heidi for her birthday?", image: UIImage(named: "Profile2")))
items.append(Item(text: "Brunch this weekend?", detail: "I'll be in your neighborhood doing errands this weekend.", image: UIImage(named: "Profile3"))) items.append(Item(text: "Brunch this weekend?", detail: "I'll be in your neighborhood doing errands this weekend.", image: UIImage(named: "Profile3")))
...@@ -106,6 +101,7 @@ class InboxViewController: UIViewController { ...@@ -106,6 +101,7 @@ class InboxViewController: UIViewController {
/// Prepares the tableView. /// Prepares the tableView.
private func prepareTableView() { private func prepareTableView() {
tableView = UITableView()
tableView.registerClass(MaterialTableViewCell.self, forCellReuseIdentifier: "MaterialTableViewCell") tableView.registerClass(MaterialTableViewCell.self, forCellReuseIdentifier: "MaterialTableViewCell")
tableView.dataSource = self tableView.dataSource = self
tableView.delegate = self tableView.delegate = self
...@@ -115,6 +111,22 @@ class InboxViewController: UIViewController { ...@@ -115,6 +111,22 @@ class InboxViewController: UIViewController {
tableView.translatesAutoresizingMaskIntoConstraints = false tableView.translatesAutoresizingMaskIntoConstraints = false
MaterialLayout.alignToParent(view, child: tableView) MaterialLayout.alignToParent(view, child: tableView)
} }
/// Prepares the titleLabel.
private func prepareTitleLabel() {
titleLabel = UILabel()
titleLabel.text = "Messages"
titleLabel.textAlignment = .Left
titleLabel.textColor = MaterialColor.white
}
/// Prepares the detailLabel.
private func prepareDetailLabel() {
detailLabel = UILabel()
detailLabel.text = "\(items.count) Items"
detailLabel.textAlignment = .Left
detailLabel.textColor = MaterialColor.white
}
} }
/// TableViewDataSource methods. /// TableViewDataSource methods.
......
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "self:NavigationBar.xcodeproj">
</FileRef>
</Workspace>
/*
* Copyright (C) 2015 - 2016, Daniel Dahan and CosmicMind, Inc. <http://cosmicmind.io>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Material nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window!.rootViewController = ViewController()
window!.makeKeyAndVisible()
return true
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
{
"images" : [
{
"size" : "29x29",
"idiom" : "iphone",
"filename" : "Material-Icon-29@2x.png",
"scale" : "2x"
},
{
"size" : "29x29",
"idiom" : "iphone",
"filename" : "Material-Icon-29@3x.png",
"scale" : "3x"
},
{
"size" : "40x40",
"idiom" : "iphone",
"filename" : "Material-Icon-40@2x-1.png",
"scale" : "2x"
},
{
"size" : "40x40",
"idiom" : "iphone",
"filename" : "Material-Icon-40@3x.png",
"scale" : "3x"
},
{
"size" : "60x60",
"idiom" : "iphone",
"filename" : "Material-Icon-60@2x.png",
"scale" : "2x"
},
{
"size" : "60x60",
"idiom" : "iphone",
"filename" : "Material-Icon-60@3x.png",
"scale" : "3x"
},
{
"size" : "29x29",
"idiom" : "ipad",
"filename" : "Material-Icon-29.png",
"scale" : "1x"
},
{
"size" : "29x29",
"idiom" : "ipad",
"filename" : "Material-Icon-29@2x-1.png",
"scale" : "2x"
},
{
"size" : "40x40",
"idiom" : "ipad",
"filename" : "Material-Icon-40.png",
"scale" : "1x"
},
{
"size" : "40x40",
"idiom" : "ipad",
"filename" : "Material-Icon-40@2x.png",
"scale" : "2x"
},
{
"size" : "76x76",
"idiom" : "ipad",
"filename" : "Material-Icon-76.png",
"scale" : "1x"
},
{
"size" : "76x76",
"idiom" : "ipad",
"filename" : "Material-Icon-76@2x.png",
"scale" : "2x"
},
{
"size" : "83.5x83.5",
"idiom" : "ipad",
"filename" : "Material-Icon-83.5@2x.png",
"scale" : "2x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8150" systemVersion="15A204g" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8122"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="EHf-IW-A2E">
<objects>
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="Llm-lL-Icb"/>
<viewControllerLayoutGuide type="bottom" id="xb3-aO-Qok"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<animations/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="53" y="375"/>
</scene>
</scenes>
</document>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
<string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</dict>
</plist>
/*
* Copyright (C) 2015 - 2016, Daniel Dahan and CosmicMind, Inc. <http://cosmicmind.io>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Material nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import UIKit
import Material
class ViewController: UIViewController {
/// NavigationBar title label.
private var titleLabel: UILabel!
/// NavigationBar menu button.
private var menuButton: FlatButton!
/// NavigationBar switch control.
private var switchControl: MaterialSwitch!
/// NavigationBar search button.
private var searchButton: FlatButton!
/// Reference for NavigationBar.
private var navigationBar: NavigationBar!
override func viewDidLoad() {
super.viewDidLoad()
prepareView()
prepareTitleLabel()
prepareMenuButton()
prepareSwitchControl()
prepareSearchButton()
prepareNavigationBar()
}
/// General preparation statements.
private func prepareView() {
view.backgroundColor = MaterialColor.white
}
/// Prepares the titleLabel.
private func prepareTitleLabel() {
titleLabel = UILabel()
titleLabel.text = "Inbox"
titleLabel.textAlignment = .Left
titleLabel.textColor = MaterialColor.white
}
/// Prepares the menuButton.
private func prepareMenuButton() {
let image: UIImage? = MaterialIcon.menu
menuButton = FlatButton()
menuButton.pulseScale = false
menuButton.pulseColor = MaterialColor.white
menuButton.setImage(image, forState: .Normal)
menuButton.setImage(image, forState: .Highlighted)
}
/// Prepares the switchControl.
private func prepareSwitchControl() {
switchControl = MaterialSwitch(state: .Off, style: .LightContent, size: .Small)
}
/// Prepares the searchButton.
private func prepareSearchButton() {
let image: UIImage? = MaterialIcon.search
searchButton = FlatButton()
searchButton.pulseScale = false
searchButton.pulseColor = MaterialColor.white
searchButton.setImage(image, forState: .Normal)
searchButton.setImage(image, forState: .Highlighted)
}
/// Prepare navigationBar.
private func prepareNavigationBar() {
navigationBar = NavigationBar()
navigationBar.backgroundColor = MaterialColor.blue.base
navigationBar.tintColor = MaterialColor.white
let item: UINavigationItem = UINavigationItem()
item.titleLabel = titleLabel
item.leftControls = [menuButton]
item.rightControls = [switchControl, searchButton]
navigationBar.pushNavigationItem(item, animated: true)
view.addSubview(navigationBar)
navigationBar.translatesAutoresizingMaskIntoConstraints = false
MaterialLayout.alignFromTop(view, child: navigationBar)
MaterialLayout.alignToParentHorizontally(view, child: navigationBar)
}
}
{
"images" : [
{
"idiom" : "universal",
"filename" : "ic_add_white.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "ic_add_white_2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "ic_add_white_3x.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
{
"images" : [
{
"idiom" : "universal",
"filename" : "ic_menu_white.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "ic_menu_white_2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "ic_menu_white_3x.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
{
"images" : [
{
"idiom" : "universal",
"filename" : "ic_search_white.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "ic_search_white_2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "ic_search_white_3x.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
{
"images" : [
{
"idiom" : "universal",
"filename" : "ic_share_white.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "ic_share_white_2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "ic_share_white_3x.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
...@@ -47,7 +47,7 @@ public struct MaterialDevice { ...@@ -47,7 +47,7 @@ public struct MaterialDevice {
return .iPhone return .iPhone
case .TV: case .TV:
return .TV return .TV
case .Unspecified: default:
return .Unspecified return .Unspecified
} }
} }
......
...@@ -31,9 +31,12 @@ ...@@ -31,9 +31,12 @@
import UIKit import UIKit
public struct MaterialIcon { public struct MaterialIcon {
public static let add: UIImage? = UIImage(named: "ic_add_white", inBundle: NSBundle(identifier: "io.cosmicmind.Material"), compatibleWithTraitCollection: nil)?.imageWithRenderingMode(.AlwaysTemplate)
public static let arrowBack: UIImage? = UIImage(named: "ic_arrow_back_white", inBundle: NSBundle(identifier: "io.cosmicmind.Material"), compatibleWithTraitCollection: nil)?.imageWithRenderingMode(.AlwaysTemplate) public static let arrowBack: UIImage? = UIImage(named: "ic_arrow_back_white", inBundle: NSBundle(identifier: "io.cosmicmind.Material"), compatibleWithTraitCollection: nil)?.imageWithRenderingMode(.AlwaysTemplate)
public static let arrowDownward: UIImage? = UIImage(named: "ic_arrow_downward_white", inBundle: NSBundle(identifier: "io.cosmicmind.Material"), compatibleWithTraitCollection: nil)?.imageWithRenderingMode(.AlwaysTemplate) public static let arrowDownward: UIImage? = UIImage(named: "ic_arrow_downward_white", inBundle: NSBundle(identifier: "io.cosmicmind.Material"), compatibleWithTraitCollection: nil)?.imageWithRenderingMode(.AlwaysTemplate)
public static let clear: UIImage? = UIImage(named: "ic_close_white", inBundle: NSBundle(identifier: "io.cosmicmind.Material"), compatibleWithTraitCollection: nil)?.imageWithRenderingMode(.AlwaysTemplate)
public static let close: UIImage? = UIImage(named: "ic_close_white", inBundle: NSBundle(identifier: "io.cosmicmind.Material"), compatibleWithTraitCollection: nil)?.imageWithRenderingMode(.AlwaysTemplate) public static let close: UIImage? = UIImage(named: "ic_close_white", inBundle: NSBundle(identifier: "io.cosmicmind.Material"), compatibleWithTraitCollection: nil)?.imageWithRenderingMode(.AlwaysTemplate)
public static let menu: UIImage? = UIImage(named: "ic_menu_white", inBundle: NSBundle(identifier: "io.cosmicmind.Material"), compatibleWithTraitCollection: nil)?.imageWithRenderingMode(.AlwaysTemplate)
public static let search: UIImage? = UIImage(named: "ic_search_white", inBundle: NSBundle(identifier: "io.cosmicmind.Material"), compatibleWithTraitCollection: nil)?.imageWithRenderingMode(.AlwaysTemplate)
public static let share: UIImage? = UIImage(named: "ic_share_white", inBundle: NSBundle(identifier: "io.cosmicmind.Material"), compatibleWithTraitCollection: nil)?.imageWithRenderingMode(.AlwaysTemplate)
} }
...@@ -235,89 +235,131 @@ public class NavigationBar : UINavigationBar { ...@@ -235,89 +235,131 @@ public class NavigationBar : UINavigationBar {
public override func layoutSubviews() { public override func layoutSubviews() {
super.layoutSubviews() super.layoutSubviews()
if let item: UINavigationItem = topItem {
/* layoutNavigationItem(item)
When rotating the device orientation, this adjusts the layout sizeNavigationItem(item)
of the titleView subviews. }
*/
topItem?.titleView?.grid.reloadLayout()
} }
/** /**
Lays out the UINavigationItem. Lays out the UINavigationItem.
- Parameter item: A UINavigationItem to layout. - Parameter item: A UINavigationItem to layout.
*/ */
public func layoutNavigationItem(item: UINavigationItem) { internal func layoutNavigationItem(item: UINavigationItem) {
prepareItem(item)
// We only want to work with the intrinsic height.
let inset: CGFloat = MaterialDevice.landscape ? item.landscapeInset : item.portraitInset
// leftControls // leftControls
if let v: Array<UIControl> = item.leftControls { if let v: Array<UIControl> = item.leftControls {
var n: Array<UIBarButtonItem> = Array<UIBarButtonItem>() var n: Array<UIBarButtonItem> = Array<UIBarButtonItem>()
for c in v { for c in v {
c.bounds.size = c is MaterialSwitch ? backButton.bounds.size : c.intrinsicContentSize()
n.append(UIBarButtonItem(customView: c)) n.append(UIBarButtonItem(customView: c))
} }
// The spacer moves the UIBarButtonItems to the edge of the UINavigationBar. // The spacer moves the UIBarButtonItems to the edge of the UINavigationBar.
let spacer: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .FixedSpace, target: nil, action: nil) let spacer: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .FixedSpace, target: nil, action: nil)
spacer.width = item.inset spacer.width = inset
n.append(spacer) n.append(spacer)
item.leftBarButtonItems = n.reverse() item.leftBarButtonItems = n.reverse()
} }
if nil == item.titleView { let titleView: UIView = UIView()
item.titleView = UIView(frame: CGRectMake(0, 2, MaterialDevice.width < MaterialDevice.height ? MaterialDevice.height : MaterialDevice.width, 40)) titleView.grid.views = []
item.titleView!.backgroundColor = nil titleView.backgroundColor = nil
item.titleView!.grid.axis.direction = .Vertical titleView.grid.axis.direction = .Vertical
}
item.titleView!.grid.views = []
// TitleView alignment. // TitleView alignment.
if let t: UILabel = item.titleLabel { if let t: UILabel = item.titleLabel {
t.grid.rows = 1 t.backgroundColor = MaterialColor.red.accent1
titleView.addSubview(t)
item.titleView!.addSubview(t) titleView.grid.views?.append(t)
item.titleView!.grid.views?.append(t)
if let d: UILabel = item.detailLabel { if let d: UILabel = item.detailLabel {
d.grid.rows = 1 d.backgroundColor = MaterialColor.red.accent3
d.font = d.font.fontWithSize(12) titleView.addSubview(d)
titleView.grid.views?.append(d)
t.font = t.font.fontWithSize(17)
item.titleView!.addSubview(d)
item.titleView!.grid.axis.rows = 2
item.titleView!.grid.views?.append(d)
} else {
t.font = t.font?.fontWithSize(20)
item.titleView!.grid.axis.rows = 1
} }
} else if let d: UIView = item.detailView { } else if let d: UIView = item.detailView {
d.grid.rows = 1 titleView.addSubview(d)
titleView.grid.views?.append(d)
item.titleView!.addSubview(d)
item.titleView!.grid.axis.rows = 1
item.titleView!.grid.views?.append(d)
} }
// rightControls // rightControls
if let v: Array<UIControl> = item.rightControls { if let v: Array<UIControl> = item.rightControls {
var n: Array<UIBarButtonItem> = Array<UIBarButtonItem>() var n: Array<UIBarButtonItem> = Array<UIBarButtonItem>()
for c in v { for c in v {
c.bounds.size = c is MaterialSwitch ? backButton.bounds.size : c.intrinsicContentSize()
n.append(UIBarButtonItem(customView: c)) n.append(UIBarButtonItem(customView: c))
} }
// The spacer moves the UIBarButtonItems to the edge of the UINavigationBar. // The spacer moves the UIBarButtonItems to the edge of the UINavigationBar.
let spacer: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .FixedSpace, target: nil, action: nil) let spacer: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .FixedSpace, target: nil, action: nil)
spacer.width = item.inset spacer.width = inset
n.append(spacer) n.append(spacer)
item.rightBarButtonItems = n.reverse() item.rightBarButtonItems = n.reverse()
} }
item.titleView!.grid.reloadLayout() item.titleView = titleView
}
/**
Sizes out the UINavigationItem.
- Parameter item: A UINavigationItem to layout.
*/
internal func sizeNavigationItem(item: UINavigationItem) {
let h: CGFloat = height
// leftControls
if let v: Array<UIControl> = item.leftControls {
for c in v {
if let b: UIButton = c as? UIButton {
b.contentEdgeInsets.top = 0
b.contentEdgeInsets.bottom = 0
}
c.bounds.size = c is MaterialSwitch ? CGSizeMake(backButton.bounds.width, h - 4) : CGSizeMake(c.intrinsicContentSize().width, h - 4)
}
}
if let titleView: UIView = item.titleView {
titleView.frame.size.height = h - 4
// TitleView alignment.
if let t: UILabel = item.titleLabel {
t.grid.rows = 1
if 32 >= h || nil == item.detailLabel {
t.font = t.font?.fontWithSize(20)
titleView.grid.axis.rows = 1
item.detailLabel?.hidden = true
} else if let d: UILabel = item.detailLabel {
t.font = t.font.fontWithSize(17)
d.font = d.font.fontWithSize(12)
d.grid.rows = 1
d.hidden = false
titleView.grid.axis.rows = 2
}
} else if let d: UIView = item.detailView {
d.grid.rows = 1
titleView.grid.axis.rows = 1
}
titleView.grid.reloadLayout()
}
// rightControls
if let v: Array<UIControl> = item.rightControls {
for c in v {
if let b: UIButton = c as? UIButton {
b.contentEdgeInsets.top = 0
b.contentEdgeInsets.bottom = 0
}
print(c.intrinsicContentSize())
c.bounds.size = c is MaterialSwitch ? CGSizeMake(backButton.intrinsicContentSize().width, h - 4) : CGSizeMake(c.intrinsicContentSize().width, h - 4)
}
}
} }
/** /**
...@@ -343,14 +385,24 @@ public class NavigationBar : UINavigationBar { ...@@ -343,14 +385,24 @@ public class NavigationBar : UINavigationBar {
backButton.setImage(backButtonImage, forState: .Normal) backButton.setImage(backButtonImage, forState: .Normal)
backButton.setImage(backButtonImage, forState: .Highlighted) backButton.setImage(backButtonImage, forState: .Highlighted)
} }
/// Prepares the UINavigationItem for layout and sizing.
internal func prepareItem(item: UINavigationItem) {
if nil == item.title {
item.title = ""
}
}
} }
/// A memory reference to the NavigationItem instance for UINavigationBar extensions. /// A memory reference to the NavigationItem instance for UINavigationBar extensions.
private var NavigationItemKey: UInt8 = 0 private var NavigationItemKey: UInt8 = 0
public class NavigationItem { public class NavigationItem {
/// Inset. /// Portrait Inset.
public var inset: CGFloat = -16 public var portraitInset: CGFloat = -16
/// Landscape Inset.
public var landscapeInset: CGFloat = -20
/// Detail View. /// Detail View.
public var detailView: UIView? public var detailView: UIView?
...@@ -381,13 +433,23 @@ public extension UINavigationItem { ...@@ -381,13 +433,23 @@ public extension UINavigationItem {
} }
} }
/// Inset. /// Portrait Inset.
public var inset: CGFloat { public var portraitInset: CGFloat {
get {
return item.portraitInset
}
set(value) {
item.portraitInset = value
}
}
/// Landscape Inset.
public var landscapeInset: CGFloat {
get { get {
return item.inset return item.landscapeInset
} }
set(value) { set(value) {
item.inset = value item.landscapeInset = value
} }
} }
......
...@@ -66,9 +66,11 @@ public class NavigationController : UINavigationController { ...@@ -66,9 +66,11 @@ public class NavigationController : UINavigationController {
public override func viewDidAppear(animated: Bool) { public override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated) super.viewDidAppear(animated)
if let v: UINavigationItem = navigationBar.topItem { if let v: UINavigationItem = navigationBar.topItem {
prepareTitle(v) if let b: NavigationBar = navigationBar as? NavigationBar {
(navigationBar as? NavigationBar)?.layoutNavigationItem(v) b.layoutNavigationItem(v)
}
} }
} }
...@@ -82,7 +84,6 @@ public class NavigationController : UINavigationController { ...@@ -82,7 +84,6 @@ public class NavigationController : UINavigationController {
*/ */
public func navigationBar(navigationBar: UINavigationBar, shouldPushItem item: UINavigationItem) -> Bool { public func navigationBar(navigationBar: UINavigationBar, shouldPushItem item: UINavigationItem) -> Bool {
if let v: NavigationBar = navigationBar as? NavigationBar { if let v: NavigationBar = navigationBar as? NavigationBar {
prepareTitle(item)
item.setHidesBackButton(true, animated: false) item.setHidesBackButton(true, animated: false)
if var c: Array<UIControl> = item.leftControls { if var c: Array<UIControl> = item.leftControls {
c.append(v.backButton) c.append(v.backButton)
...@@ -101,11 +102,4 @@ public class NavigationController : UINavigationController { ...@@ -101,11 +102,4 @@ public class NavigationController : UINavigationController {
internal func handleBackButton() { internal func handleBackButton() {
popViewControllerAnimated(true) popViewControllerAnimated(true)
} }
/// Prepares the title if it's value is nil.
private func prepareTitle(item: UINavigationItem) {
if nil == item.title {
item.title = ""
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment