Commit 77e68079 by Daniel Dahan

added custom leftControls and rightControls

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