Commit 77e68079 by Daniel Dahan

added custom leftControls and rightControls

parent 3f8dc4cc
......@@ -52,10 +52,10 @@ class FeedViewController: UIViewController {
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.statusBarStyle = .LightContent
navigationController?.navigationBar.backgroundColor = MaterialColor.blue.base
let image = UIImage(named: "ic_menu_white")
navigationItem.title = "Feed"
var image = UIImage(named: "ic_menu_white")
// Menu button.
let menuButton: FlatButton = FlatButton()
......@@ -65,16 +65,36 @@ class FeedViewController: UIViewController {
menuButton.setImage(image, forState: .Highlighted)
menuButton.addTarget(self, action: "handleMenuButton", forControlEvents: .TouchUpInside)
navigationController?.navigationBar.rightControls = [menuButton]
// Switch control.
let switchControl: MaterialSwitch = MaterialSwitch(state: .Off, style: .LightContent, size: .Small)
// Search button.
image = UIImage(named: "ic_search_white")
let searchButton: FlatButton = FlatButton()
searchButton.pulseColor = MaterialColor.white
searchButton.pulseScale = false
searchButton.setImage(image, forState: .Normal)
searchButton.setImage(image, forState: .Highlighted)
searchButton.addTarget(self, action: "handleSearchButton", forControlEvents: .TouchUpInside)
navigationController?.navigationBar.leftControls = [menuButton]
navigationController?.navigationBar.rightControls = [switchControl, searchButton]
}
internal func handleMenuButton() {
print("handled")
}
internal func handleSearchButton() {
print("handled")
}
/// Prepares view.
private func prepareView() {
view.backgroundColor = MaterialColor.grey.lighten4
navigationController?.navigationBar.statusBarStyle = .LightContent
navigationController?.navigationBar.backgroundColor = MaterialColor.blue.base
}
/// Prepares the collectionView
......
......@@ -43,6 +43,11 @@ class ViewController: UIViewController {
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
navigationItem.title = "Material"
navigationController?.navigationBar.leftControls = []
navigationController?.navigationBar.rightControls = []
}
/// Prepares view.
......
......@@ -34,15 +34,26 @@ import UIKit
private var NavigationBarKey: UInt8 = 0
public class Controls {
/// A preset wrapper around contentInset.
public var contentInsetPreset: MaterialEdgeInset
/// A wrapper around grid.contentInset.
public var contentInset: UIEdgeInsets
/// Left controls.
public var leftControls: Array<UIControl>?
/// Right controls.
public var rightControls: Array<UIControl>?
public init() {
contentInsetPreset = .Square3
contentInset = MaterialEdgeInsetToValue(.Square3)
}
}
public extension UINavigationBar {
/// Grid reference.
/// Controls reference.
public var controls: Controls {
get {
return MaterialObjectAssociatedObject(self, key: &NavigationBarKey) {
......@@ -54,15 +65,6 @@ public extension UINavigationBar {
}
}
public var title: String? {
get {
return topItem?.title
}
set(value) {
topItem?.title = value
}
}
/// Device status bar style.
public var statusBarStyle: UIStatusBarStyle {
get {
......@@ -81,14 +83,22 @@ public extension UINavigationBar {
var c: Array<UIBarButtonItem> = Array<UIBarButtonItem>()
if let v: Array<UIControl> = value {
for q in v {
let b: UIBarButtonItem = UIBarButtonItem(customView: q)
b.width = q.intrinsicContentSize().width
c.append(b)
if let p: UIButton = q as? UIButton {
p.frame.size = CGSizeMake(40, 28)
p.contentEdgeInsets = UIEdgeInsetsZero
c.append(UIBarButtonItem(customView: p))
} else {
c.append(UIBarButtonItem(customView: q))
}
}
}
let spacer: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .FixedSpace, target: nil, action: nil)
spacer.width = -20 + controls.contentInset.left
c.append(spacer)
controls.leftControls = value
topItem?.leftBarButtonItems = c
topItem?.leftBarButtonItems = c.reverse()
}
}
......@@ -100,19 +110,47 @@ public extension UINavigationBar {
var c: Array<UIBarButtonItem> = Array<UIBarButtonItem>()
if let v: Array<UIControl> = value {
for q in v {
let b: UIBarButtonItem = UIBarButtonItem(customView: q)
b.width = q.intrinsicContentSize().width
c.append(b)
if let p: UIButton = q as? UIButton {
p.frame.size = CGSizeMake(40, 28)
p.contentEdgeInsets = UIEdgeInsetsZero
c.append(UIBarButtonItem(customView: p))
} else {
c.append(UIBarButtonItem(customView: q))
}
}
}
let spacer: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .FixedSpace, target: nil, action: nil)
spacer.width = -20 + controls.contentInset.right
c.append(spacer)
controls.rightControls = value
topItem?.rightBarButtonItems = c
topItem?.rightBarButtonItems = c.reverse()
}
}
}
public class NavigationBar : UINavigationBar {
/// A preset wrapper around contentInset.
public var contentInsetPreset: MaterialEdgeInset {
get {
return controls.contentInsetPreset
}
set(value) {
controls.contentInsetPreset = value
}
}
/// A wrapper around grid.contentInset.
public var contentInset: UIEdgeInsets {
get {
return controls.contentInset
}
set(value) {
controls.contentInset = value
}
}
/**
The back button image writes to the backIndicatorImage property and
backIndicatorTransitionMaskImage property.
......@@ -223,5 +261,11 @@ public class NavigationBar : UINavigationBar {
backButtonImage = nil
backgroundColor = MaterialColor.white
depth = .Depth1
titleTextAttributes = [NSFontAttributeName: RobotoFont.regularWithSize(20)]
}
public func reloadView() {
leftControls = controls.leftControls
rightControls = controls.rightControls
}
}
......@@ -56,4 +56,11 @@ public class NavigationController : UINavigationController {
public func prepareView() {
navigationItem.title = "Title"
}
public override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
if let v: NavigationBar = navigationBar as? NavigationBar {
v.reloadView()
}
}
}
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