Commit 77eee758 by Daniel Dahan

development: updated NavigationBar contentView and titleView management

parent 81927cf0
...@@ -73,7 +73,7 @@ public class Layout { ...@@ -73,7 +73,7 @@ public class Layout {
- Returns: The current Layout instance. - Returns: The current Layout instance.
*/ */
internal func debugChildNotAvailableMessage(function: String = #function) -> Layout { internal func debugChildNotAvailableMessage(function: String = #function) -> Layout {
debugPrint("[Material Layout Error: Chld view context is not available for \(function).") debugPrint("[Material Layout Error: Child view context is not available for \(function).")
return self return self
} }
...@@ -854,7 +854,7 @@ extension Layout { ...@@ -854,7 +854,7 @@ extension Layout {
- Parameter views: A Dictionary<String, Any> of view key / value pairs. - Parameter views: A Dictionary<String, Any> of view key / value pairs.
- Returns: The Array<NSLayoutConstraint> instance. - Returns: The Array<NSLayoutConstraint> instance.
*/ */
public class func constraint(format: String, options: NSLayoutFormatOptions, metrics: Dictionary<String, Any>?, views: Dictionary<String, Any>) -> Array<NSLayoutConstraint> { public class func constraint(format: String, options: NSLayoutFormatOptions, metrics: Dictionary<String, Any>?, views: Dictionary<String, Any>) -> [NSLayoutConstraint] {
for (_, a) in views { for (_, a) in views {
if let v = a as? UIView { if let v = a as? UIView {
v.translatesAutoresizingMaskIntoConstraints = false v.translatesAutoresizingMaskIntoConstraints = false
......
...@@ -192,12 +192,10 @@ open class NavigationBar: UINavigationBar { ...@@ -192,12 +192,10 @@ open class NavigationBar: UINavigationBar {
internal func layoutNavigationItem(item: UINavigationItem) { internal func layoutNavigationItem(item: UINavigationItem) {
if willRenderView { if willRenderView {
prepareItem(item: item) prepareItem(item: item)
prepareTitleView(item: item)
let titleView = prepareTitleView(item: item) item.titleView!.frame.origin = .zero
let contentView = prepareContentView(item: item) item.titleView!.frame.size = intrinsicContentSize
titleView.frame.origin = .zero
titleView.frame.size = intrinsicContentSize
var lc = 0 var lc = 0
var rc = 0 var rc = 0
...@@ -206,9 +204,9 @@ open class NavigationBar: UINavigationBar { ...@@ -206,9 +204,9 @@ open class NavigationBar: UINavigationBar {
let p = width - l - r - contentEdgeInsets.left - contentEdgeInsets.right let p = width - l - r - contentEdgeInsets.left - contentEdgeInsets.right
let columns = Int(p / gridFactor) let columns = Int(p / gridFactor)
titleView.grid.deferred = true item.titleView!.grid.deferred = true
titleView.grid.views.removeAll() item.titleView!.grid.views.removeAll()
titleView.grid.axis.columns = columns item.titleView!.grid.axis.columns = columns
for v in item.leftControls { for v in item.leftControls {
(v as? UIButton)?.contentEdgeInsets = .zero (v as? UIButton)?.contentEdgeInsets = .zero
...@@ -217,10 +215,10 @@ open class NavigationBar: UINavigationBar { ...@@ -217,10 +215,10 @@ open class NavigationBar: UINavigationBar {
lc += v.grid.columns lc += v.grid.columns
titleView.grid.views.append(v) item.titleView!.grid.views.append(v)
} }
titleView.grid.views.append(contentView) item.titleView!.grid.views.append(item.contentView)
for v in item.rightControls { for v in item.rightControls {
(v as? UIButton)?.contentEdgeInsets = .zero (v as? UIButton)?.contentEdgeInsets = .zero
...@@ -229,61 +227,61 @@ open class NavigationBar: UINavigationBar { ...@@ -229,61 +227,61 @@ open class NavigationBar: UINavigationBar {
rc += v.grid.columns rc += v.grid.columns
titleView.grid.views.append(v) item.titleView!.grid.views.append(v)
} }
if .center == item.contentViewAlignment { if .center == item.contentViewAlignment {
if lc < rc { if lc < rc {
contentView.grid.columns = columns - 2 * rc item.contentView.grid.columns = columns - 2 * rc
contentView.grid.offset.columns = rc - lc item.contentView.grid.offset.columns = rc - lc
} else { } else {
contentView.grid.columns = columns - 2 * lc item.contentView.grid.columns = columns - 2 * lc
item.rightControls.first?.grid.offset.columns = lc - rc item.rightControls.first?.grid.offset.columns = lc - rc
} }
} else { } else {
contentView.grid.columns = columns - lc - rc item.contentView.grid.columns = columns - lc - rc
} }
titleView.grid.interimSpace = interimSpace item.titleView!.grid.interimSpace = interimSpace
titleView.grid.contentEdgeInsets = contentEdgeInsets item.titleView!.grid.contentEdgeInsets = contentEdgeInsets
titleView.grid.deferred = false item.titleView!.grid.deferred = false
titleView.grid.reload() item.titleView!.grid.reload()
// contentView alignment. // contentView alignment.
if nil != item.title && "" != item.title { if nil != item.title && "" != item.title {
if nil == item.titleLabel.superview { if nil == item.titleLabel.superview {
contentView.addSubview(item.titleLabel) item.contentView.addSubview(item.titleLabel)
} }
item.titleLabel.frame = contentView.bounds item.titleLabel.frame = item.contentView.bounds
} else { } else {
item.titleLabel.removeFromSuperview() item.titleLabel.removeFromSuperview()
} }
if nil != item.detail && "" != item.detail { if nil != item.detail && "" != item.detail {
if nil == item.detailLabel.superview { if nil == item.detailLabel.superview {
contentView.addSubview(item.detailLabel) item.contentView.addSubview(item.detailLabel)
} }
if nil == item.titleLabel.superview { if nil == item.titleLabel.superview {
item.detailLabel.frame = contentView.bounds item.detailLabel.frame = item.contentView.bounds
} else { } else {
item.titleLabel.sizeToFit() item.titleLabel.sizeToFit()
item.detailLabel.sizeToFit() item.detailLabel.sizeToFit()
let diff = (contentView.height - item.titleLabel.height - item.detailLabel.height) / 2 let diff = (item.contentView.height - item.titleLabel.height - item.detailLabel.height) / 2
item.titleLabel.height += diff item.titleLabel.height += diff
item.titleLabel.width = contentView.width item.titleLabel.width = item.contentView.width
item.detailLabel.height += diff item.detailLabel.height += diff
item.detailLabel.width = contentView.width item.detailLabel.width = item.contentView.width
item.detailLabel.y = item.titleLabel.height item.detailLabel.y = item.titleLabel.height
} }
} else { } else {
item.detailLabel.removeFromSuperview() item.detailLabel.removeFromSuperview()
} }
contentView.grid.reload() item.contentView.grid.reload()
} }
} }
...@@ -321,27 +319,14 @@ open class NavigationBar: UINavigationBar { ...@@ -321,27 +319,14 @@ open class NavigationBar: UINavigationBar {
/** /**
Prepare the titleView. Prepare the titleView.
- Parameter item: A UINavigationItem to layout. - Parameter item: A UINavigationItem to layout.
- Returns: A UIView, which is the item.titleView.
*/ */
private func prepareTitleView(item: UINavigationItem) -> UIView { private func prepareTitleView(item: UINavigationItem) {
if nil == item.titleView { guard nil == item.titleView else {
item.titleView = UIView(frame: .zero) return
} }
return item.titleView! item.titleView = UIView(frame: .zero)
} }
/**
Prepare the contentView.
- Parameter item: A UINavigationItem to layout.
- Returns: A UIView, which is the item.contentView.
*/
private func prepareContentView(item: UINavigationItem) -> UIView {
if nil == item.contentView {
item.contentView = UIView(frame: .zero)
}
return item.contentView!
}
/// Prepares the divider. /// Prepares the divider.
private func prepareDivider() { private func prepareDivider() {
divider = Divider(view: self) divider = Divider(view: self)
......
...@@ -46,13 +46,13 @@ public class NavigationItem: NSObject { ...@@ -46,13 +46,13 @@ public class NavigationItem: NSObject {
public var backButton: IconButton? public var backButton: IconButton?
/// Content View. /// Content View.
public var contentView: UIView? public private(set) lazy var contentView = UIView()
/// Title label. /// Title label.
public private(set) lazy var titleLabel: UILabel = UILabel() public private(set) lazy var titleLabel = UILabel()
/// Detail label. /// Detail label.
public private(set) lazy var detailLabel: UILabel = UILabel() public private(set) lazy var detailLabel = UILabel()
/// Left controls. /// Left controls.
public var leftControls = [UIView]() public var leftControls = [UIView]()
...@@ -61,7 +61,7 @@ public class NavigationItem: NSObject { ...@@ -61,7 +61,7 @@ public class NavigationItem: NSObject {
public var rightControls = [UIView]() public var rightControls = [UIView]()
public var navigationBar: NavigationBar? { public var navigationBar: NavigationBar? {
return contentView?.superview?.superview as? NavigationBar return contentView.superview?.superview as? NavigationBar
} }
open override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { open override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
...@@ -116,12 +116,17 @@ extension UINavigationItem { ...@@ -116,12 +116,17 @@ extension UINavigationItem {
} }
/// Should center the contentView. /// Should center the contentView.
open var contentViewAlignment: ContentViewAlignment { public var contentViewAlignment: ContentViewAlignment {
return navigationItem.contentViewAlignment return navigationItem.contentViewAlignment
} }
/// Content View.
public var contentView: UIView {
return navigationItem.contentView
}
/// Back Button. /// Back Button.
open internal(set) var backButton: IconButton? { public internal(set) var backButton: IconButton? {
get { get {
return navigationItem.backButton return navigationItem.backButton
} }
...@@ -130,18 +135,8 @@ extension UINavigationItem { ...@@ -130,18 +135,8 @@ extension UINavigationItem {
} }
} }
/// Content View.
open internal(set) var contentView: UIView? {
get {
return navigationItem.contentView
}
set(value) {
navigationItem.contentView = value
}
}
@nonobjc @nonobjc
open var title: String? { public var title: String? {
get { get {
return titleLabel.text return titleLabel.text
} }
...@@ -152,12 +147,12 @@ extension UINavigationItem { ...@@ -152,12 +147,12 @@ extension UINavigationItem {
} }
/// Title Label. /// Title Label.
open var titleLabel: UILabel { public var titleLabel: UILabel {
return navigationItem.titleLabel return navigationItem.titleLabel
} }
/// Detail text. /// Detail text.
open var detail: String? { public var detail: String? {
get { get {
return detailLabel.text return detailLabel.text
} }
...@@ -168,12 +163,12 @@ extension UINavigationItem { ...@@ -168,12 +163,12 @@ extension UINavigationItem {
} }
/// Detail Label. /// Detail Label.
open var detailLabel: UILabel { public var detailLabel: UILabel {
return navigationItem.detailLabel return navigationItem.detailLabel
} }
/// Left side UIViews. /// Left side UIViews.
open var leftControls: [UIView] { public var leftControls: [UIView] {
get { get {
return navigationItem.leftControls return navigationItem.leftControls
} }
...@@ -183,7 +178,7 @@ extension UINavigationItem { ...@@ -183,7 +178,7 @@ extension UINavigationItem {
} }
/// Right side UIViews. /// Right side UIViews.
open var rightControls: [UIView] { public var rightControls: [UIView] {
get { get {
return navigationItem.rightControls return navigationItem.rightControls
} }
......
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