Commit 77eee758 by Daniel Dahan

development: updated NavigationBar contentView and titleView management

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