Commit 7ddb1213 by Daniel Dahan

NavigationBar working with dynamic controls

parent ff3b0d89
...@@ -36,8 +36,7 @@ flow of your application. ...@@ -36,8 +36,7 @@ flow of your application.
import UIKit import UIKit
import Material import Material
class AppNavigationController : NavigationController { class AppNavigationController: NavigationController {
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
......
...@@ -74,12 +74,12 @@ class FeedViewController: UIViewController { ...@@ -74,12 +74,12 @@ class FeedViewController: UIViewController {
view.backgroundColor = MaterialColor.grey.lighten4 view.backgroundColor = MaterialColor.grey.lighten4
let titleLabel: UILabel = UILabel() let titleLabel: UILabel = UILabel()
titleLabel.text = "Material" titleLabel.text = "Materialyyppgg"
titleLabel.textAlignment = .Left titleLabel.textAlignment = .Left
titleLabel.textColor = MaterialColor.white titleLabel.textColor = MaterialColor.white
let detailLabel: UILabel = UILabel() let detailLabel: UILabel = UILabel()
detailLabel.text = "Build Beautiful Software" detailLabel.text = "Build Beautiful Softwareyyppgg"
detailLabel.textAlignment = .Left detailLabel.textAlignment = .Left
detailLabel.textColor = MaterialColor.white detailLabel.textColor = MaterialColor.white
......
...@@ -78,8 +78,18 @@ class InboxViewController: UIViewController { ...@@ -78,8 +78,18 @@ class InboxViewController: UIViewController {
detailLabel.textAlignment = .Left detailLabel.textAlignment = .Left
detailLabel.textColor = MaterialColor.white 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 items Array. /// Prepares the items Array.
......
...@@ -31,6 +31,9 @@ ...@@ -31,6 +31,9 @@
import UIKit import UIKit
public class NavigationBar : UINavigationBar { public class NavigationBar : UINavigationBar {
/// Reference to the backButton.
public private(set) lazy var backButton: FlatButton = FlatButton()
/** /**
A CAShapeLayer used to manage elements that would be affected by A CAShapeLayer used to manage elements that would be affected by
the clipToBounds property of the backing layer. For example, this the clipToBounds property of the backing layer. For example, this
...@@ -369,38 +372,27 @@ public class NavigationBar : UINavigationBar { ...@@ -369,38 +372,27 @@ public class NavigationBar : UINavigationBar {
} }
} }
public func layoutNavigationItem(item: UINavigationItem) { public override func layoutSubviews() {
grid.views = [] super.layoutSubviews()
grid.axis.columns = Int(width / 48) topItem?.titleView?.grid.reloadLayout()
}
let g: CGFloat = width / CGFloat(grid.axis.columns)
var columns: Int = grid.axis.columns
public func layoutNavigationItem(item: UINavigationItem) {
// 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 {
if let b: UIButton = c as? UIButton { c.bounds.size = c is MaterialSwitch ? backButton.bounds.size : c.intrinsicContentSize()
b.contentEdgeInsets = UIEdgeInsetsZero
}
c.grid.columns = 0 == g ? 1 : Int(ceil(c.intrinsicContentSize().width / g))
columns -= c.grid.columns
grid.views!.append(c)
n.append(UIBarButtonItem(customView: c)) n.append(UIBarButtonItem(customView: c))
} }
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 = 0 n.append(spacer)
// c.append(spacer)
item.leftBarButtonItems = n.reverse() item.leftBarButtonItems = n.reverse()
} }
if nil == item.titleView { if nil == item.titleView {
item.titleView = UIView() item.titleView = UIView(frame: CGRectMake(0, 2, 2000, 40))
item.titleView!.backgroundColor = nil item.titleView!.backgroundColor = nil
item.titleView!.grid.axis.direction = .Vertical item.titleView!.grid.axis.direction = .Vertical
} }
...@@ -409,56 +401,36 @@ public class NavigationBar : UINavigationBar { ...@@ -409,56 +401,36 @@ public class NavigationBar : UINavigationBar {
// TitleView alignment. // TitleView alignment.
if let t: UILabel = item.titleLabel { if let t: UILabel = item.titleLabel {
t.grid.rows = 1
item.titleView!.addSubview(t) item.titleView!.addSubview(t)
item.titleView!.grid.views?.append(t) item.titleView!.grid.views?.append(t)
if let d: UILabel = item.detailLabel { if let d: UILabel = item.detailLabel {
t.grid.rows = 2
t.font = t.font.fontWithSize(17) t.font = t.font.fontWithSize(17)
d.grid.rows = 2 d.grid.rows = 1
d.font = d.font.fontWithSize(12) d.font = d.font.fontWithSize(12)
item.titleView!.addSubview(d) item.titleView!.addSubview(d)
item.titleView!.grid.views?.append(d) item.titleView!.grid.views?.append(d)
item.titleView!.grid.axis.rows = 3 item.titleView!.grid.axis.rows = 2
item.titleView!.grid.spacing = -8
item.titleView!.grid.contentInset.top = -8
} else { } else {
t.grid.rows = 1
t.font = t.font?.fontWithSize(20) t.font = t.font?.fontWithSize(20)
item.titleView!.grid.axis.rows = 1 item.titleView!.grid.axis.rows = 1
item.titleView!.grid.spacing = 0
item.titleView!.grid.contentInset.top = 0
} }
} }
grid.views?.append(item.titleView!)
// 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 {
if let b: UIButton = c as? UIButton { c.bounds.size = c is MaterialSwitch ? backButton.bounds.size : c.intrinsicContentSize()
b.contentEdgeInsets = UIEdgeInsetsZero
}
c.grid.columns = 0 == g ? 1 : Int(ceil(c.intrinsicContentSize().width / g))
columns -= c.grid.columns
grid.views!.append(c)
n.append(UIBarButtonItem(customView: c)) n.append(UIBarButtonItem(customView: c))
} }
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 = 0 n.append(spacer)
// c.append(spacer)
item.rightBarButtonItems = n.reverse() item.rightBarButtonItems = n.reverse()
} }
item.titleView!.grid.columns = columns item.titleView!.grid.reloadLayout()
grid.reloadLayout()
item.titleView?.grid.reloadLayout()
} }
/** /**
...@@ -469,7 +441,6 @@ public class NavigationBar : UINavigationBar { ...@@ -469,7 +441,6 @@ public class NavigationBar : UINavigationBar {
when subclassing. when subclassing.
*/ */
public func prepareView() { public func prepareView() {
prepareVisualLayer()
barStyle = .Black barStyle = .Black
translucent = false translucent = false
backButtonImage = nil backButtonImage = nil
...@@ -477,6 +448,8 @@ public class NavigationBar : UINavigationBar { ...@@ -477,6 +448,8 @@ public class NavigationBar : UINavigationBar {
depth = .Depth1 depth = .Depth1
spacingPreset = .Spacing2 spacingPreset = .Spacing2
contentInset = UIEdgeInsetsMake(8, 0, 8, 0) contentInset = UIEdgeInsetsMake(8, 0, 8, 0)
prepareVisualLayer()
prepareBackButton()
} }
/// Prepares the visualLayer property. /// Prepares the visualLayer property.
...@@ -492,6 +465,14 @@ public class NavigationBar : UINavigationBar { ...@@ -492,6 +465,14 @@ public class NavigationBar : UINavigationBar {
visualLayer.cornerRadius = cornerRadius visualLayer.cornerRadius = cornerRadius
} }
/// Prepares the backButton.
internal func prepareBackButton() {
backButton.pulseScale = false
backButton.pulseColor = MaterialColor.white
backButton.setImage(backButtonImage, forState: .Normal)
backButton.setImage(backButtonImage, forState: .Highlighted)
}
/// Sets the shadow path. /// Sets the shadow path.
internal func layoutShadowPath() { internal func layoutShadowPath() {
if shadowPathAutoSizeEnabled { if shadowPathAutoSizeEnabled {
...@@ -508,6 +489,9 @@ public class NavigationBar : UINavigationBar { ...@@ -508,6 +489,9 @@ public class NavigationBar : UINavigationBar {
private var NavigationItemControlsKey: UInt8 = 0 private var NavigationItemControlsKey: UInt8 = 0
public class NavigationItemControls { public class NavigationItemControls {
/// Inset.
public var inset: CGFloat = -16
/// Left controls. /// Left controls.
public var leftControls: Array<UIControl>? public var leftControls: Array<UIControl>?
...@@ -571,6 +555,16 @@ public extension UINavigationItem { ...@@ -571,6 +555,16 @@ public extension UINavigationItem {
} }
} }
/// Inset.
public var inset: CGFloat {
get {
return controls.inset
}
set(value) {
controls.inset = value
}
}
/// Left side UIControls. /// Left side UIControls.
public var leftControls: Array<UIControl>? { public var leftControls: Array<UIControl>? {
get { get {
......
...@@ -30,9 +30,7 @@ ...@@ -30,9 +30,7 @@
import UIKit import UIKit
public class NavigationController : UINavigationController, UINavigationBarDelegate { public class NavigationController : UINavigationController {
public private(set) lazy var backButton: FlatButton = FlatButton()
public required init?(coder aDecoder: NSCoder) { public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder) super.init(coder: aDecoder)
} }
...@@ -48,7 +46,6 @@ public class NavigationController : UINavigationController, UINavigationBarDeleg ...@@ -48,7 +46,6 @@ public class NavigationController : UINavigationController, UINavigationBarDeleg
public override func viewDidLoad() { public override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
prepareBackButton()
interactivePopGestureRecognizer?.delegate = nil interactivePopGestureRecognizer?.delegate = nil
} }
...@@ -68,32 +65,21 @@ public class NavigationController : UINavigationController, UINavigationBarDeleg ...@@ -68,32 +65,21 @@ public class NavigationController : UINavigationController, UINavigationBarDeleg
public func navigationBar(navigationBar: UINavigationBar, shouldPushItem item: UINavigationItem) -> Bool { public func navigationBar(navigationBar: UINavigationBar, shouldPushItem item: UINavigationItem) -> Bool {
item.title = "" item.title = ""
item.setHidesBackButton(true, animated: false) item.setHidesBackButton(true, animated: false)
return true
}
public func navigationBar(navigationBar: UINavigationBar, didPushItem item: UINavigationItem) {
if let v: NavigationBar = navigationBar as? NavigationBar { if let v: NavigationBar = navigationBar as? NavigationBar {
if var c: Array<UIControl> = item.leftControls { if var c: Array<UIControl> = item.leftControls {
c.append(backButton) c.append(v.backButton)
item.leftControls = c item.leftControls = c
} else { } else {
item.leftControls = [backButton] item.leftControls = [v.backButton]
} }
v.backButton.removeTarget(self, action: "handleBackButton", forControlEvents: .TouchUpInside)
v.backButton.addTarget(self, action: "handleBackButton", forControlEvents: .TouchUpInside)
v.layoutNavigationItem(item) v.layoutNavigationItem(item)
} }
return true
} }
internal func handleBackButton() { internal func handleBackButton() {
popViewControllerAnimated(true) popViewControllerAnimated(true)
} }
private func prepareBackButton() {
if let v: NavigationBar = navigationBar as? NavigationBar {
backButton.pulseScale = false
backButton.pulseColor = MaterialColor.white
backButton.setImage(v.backButtonImage, forState: .Normal)
backButton.setImage(v.backButtonImage, forState: .Highlighted)
backButton.addTarget(self, action: "handleBackButton", forControlEvents: .TouchUpInside)
}
}
} }
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