Commit 17733f51 by intoxicated

Fixed iOS 11 navigationBar layoutMargin to zero

parent 0eb7acf7
...@@ -31,177 +31,183 @@ ...@@ -31,177 +31,183 @@
import UIKit import UIKit
open class NavigationBar: UINavigationBar { open class NavigationBar: UINavigationBar {
/// Will layout the view. /// Will layout the view.
open var willLayout: Bool { open var willLayout: Bool {
return 0 < bounds.width && 0 < bounds.height && nil != superview return 0 < bounds.width && 0 < bounds.height && nil != superview
}
/// Detail UILabel when in landscape for iOS 11.
fileprivate var toolbarToText: [Toolbar: String?]?
open override var intrinsicContentSize: CGSize {
return CGSize(width: bounds.width, height: bounds.height)
}
/// A preset wrapper around contentEdgeInsets.
open var contentEdgeInsetsPreset = EdgeInsetsPreset.none {
didSet {
contentEdgeInsets = EdgeInsetsPresetToValue(preset: contentEdgeInsetsPreset)
} }
}
/// Detail UILabel when in landscape for iOS 11.
fileprivate var toolbarToText: [Toolbar: String?]? /// A reference to EdgeInsets.
@IBInspectable
open override var intrinsicContentSize: CGSize { open var contentEdgeInsets = EdgeInsets.zero {
return CGSize(width: bounds.width, height: bounds.height) didSet {
layoutSubviews()
}
}
/// A preset wrapper around interimSpace.
open var interimSpacePreset = InterimSpacePreset.interimSpace3 {
didSet {
interimSpace = InterimSpacePresetToValue(preset: interimSpacePreset)
}
}
/// A wrapper around grid.interimSpace.
@IBInspectable
open var interimSpace: InterimSpace = 0 {
didSet {
layoutSubviews()
}
}
/**
The back button image writes to the backIndicatorImage property and
backIndicatorTransitionMaskImage property.
*/
@IBInspectable
open var backButtonImage: UIImage? {
get {
return backIndicatorImage
}
set(value) {
let image: UIImage? = value
backIndicatorImage = image
backIndicatorTransitionMaskImage = image
}
}
/// A property that accesses the backing layer's background
@IBInspectable
open override var backgroundColor: UIColor? {
get {
return barTintColor
} }
set(value) {
barTintColor = value
}
}
/**
An initializer that initializes the object with a NSCoder object.
- Parameter aDecoder: A NSCoder instance.
*/
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
prepare()
}
/**
An initializer that initializes the object with a CGRect object.
If AutoLayout is used, it is better to initilize the instance
using the init() initializer.
- Parameter frame: A CGRect instance.
*/
public override init(frame: CGRect) {
super.init(frame: frame)
prepare()
}
/// A convenience initializer.
public convenience init() {
self.init(frame: .zero)
}
open override func sizeThatFits(_ size: CGSize) -> CGSize {
return intrinsicContentSize
}
open override func layoutSubviews() {
super.layoutSubviews()
layoutShape()
layoutShadowPath()
/// A preset wrapper around contentEdgeInsets. //iOS 11 added left/right layout margin in subviews of UINavigationBar
open var contentEdgeInsetsPreset = EdgeInsetsPreset.none { //since we do not want to unsafely access private view directly
didSet { //iterate subviews to set `layoutMargin` to zero
contentEdgeInsets = EdgeInsetsPresetToValue(preset: contentEdgeInsetsPreset) for view in subviews {
} view.layoutMargins = .zero
} }
/// A reference to EdgeInsets. if let v = topItem {
@IBInspectable layoutNavigationItem(item: v)
open var contentEdgeInsets = EdgeInsets.zero {
didSet {
layoutSubviews()
}
} }
/// A preset wrapper around interimSpace. if let v = backItem {
open var interimSpacePreset = InterimSpacePreset.interimSpace3 { layoutNavigationItem(item: v)
didSet {
interimSpace = InterimSpacePresetToValue(preset: interimSpacePreset)
}
} }
layoutDivider()
}
/**
Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepare method
to initialize property values and other setup operations.
The super.prepare method should always be called immediately
when subclassing.
*/
open func prepare() {
barStyle = .black
isTranslucent = false
depthPreset = .none
contentScaleFactor = Screen.scale
contentEdgeInsetsPreset = .none
interimSpacePreset = .none
backButtonImage = Icon.cm.arrowBack
/// A wrapper around grid.interimSpace. if #available(iOS 11, *) {
@IBInspectable toolbarToText = [:]
open var interimSpace: InterimSpace = 0 {
didSet {
layoutSubviews()
}
} }
/**
The back button image writes to the backIndicatorImage property and
backIndicatorTransitionMaskImage property.
*/
@IBInspectable
open var backButtonImage: UIImage? {
get {
return backIndicatorImage
}
set(value) {
let image: UIImage? = value
backIndicatorImage = image
backIndicatorTransitionMaskImage = image
}
}
/// A property that accesses the backing layer's background
@IBInspectable
open override var backgroundColor: UIColor? {
get {
return barTintColor
}
set(value) {
barTintColor = value
}
}
/**
An initializer that initializes the object with a NSCoder object.
- Parameter aDecoder: A NSCoder instance.
*/
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
prepare()
}
/**
An initializer that initializes the object with a CGRect object.
If AutoLayout is used, it is better to initilize the instance
using the init() initializer.
- Parameter frame: A CGRect instance.
*/
public override init(frame: CGRect) {
super.init(frame: frame)
prepare()
}
/// A convenience initializer.
public convenience init() {
self.init(frame: .zero)
}
open override func sizeThatFits(_ size: CGSize) -> CGSize {
return intrinsicContentSize
}
open override func layoutSubviews() { let image = UIImage()
super.layoutSubviews() shadowImage = image
layoutShape() setBackgroundImage(image, for: .default)
layoutShadowPath() backgroundColor = .white
}
if let v = topItem {
layoutNavigationItem(item: v)
}
if let v = backItem {
layoutNavigationItem(item: v)
}
layoutDivider()
}
/**
Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepare method
to initialize property values and other setup operations.
The super.prepare method should always be called immediately
when subclassing.
*/
open func prepare() {
barStyle = .black
isTranslucent = false
depthPreset = .depth1
contentScaleFactor = Screen.scale
contentEdgeInsetsPreset = .square1
interimSpacePreset = .interimSpace3
backButtonImage = Icon.cm.arrowBack
if #available(iOS 11, *) {
toolbarToText = [:]
}
let image = UIImage()
shadowImage = image
setBackgroundImage(image, for: .default)
backgroundColor = .white
}
} }
internal extension NavigationBar { internal extension NavigationBar {
/** /**
Lays out the UINavigationItem. Lays out the UINavigationItem.
- Parameter item: A UINavigationItem to layout. - Parameter item: A UINavigationItem to layout.
*/ */
func layoutNavigationItem(item: UINavigationItem) { func layoutNavigationItem(item: UINavigationItem) {
guard willLayout else { guard willLayout else {
return return
} }
let toolbar = item.toolbar let toolbar = item.toolbar
toolbar.backgroundColor = .clear toolbar.backgroundColor = .clear
toolbar.interimSpace = interimSpace toolbar.interimSpace = interimSpace
toolbar.contentEdgeInsets = contentEdgeInsets toolbar.contentEdgeInsets = contentEdgeInsets
if #available(iOS 11, *) {
if Application.shouldStatusBarBeHidden {
toolbar.contentEdgeInsetsPreset = .none
if #available(iOS 11, *) { if nil != toolbar.detailLabel.text {
if Application.shouldStatusBarBeHidden { toolbarToText?[toolbar] = toolbar.detailLabel.text
toolbar.contentEdgeInsetsPreset = .none toolbar.detailLabel.text = nil
if nil != toolbar.detailLabel.text {
toolbarToText?[toolbar] = toolbar.detailLabel.text
toolbar.detailLabel.text = nil
}
} else if nil != toolbarToText?[toolbar] {
toolbar.detailLabel.text = toolbarToText?[toolbar] ?? nil
toolbarToText?[toolbar] = nil
}
} }
} else if nil != toolbarToText?[toolbar] {
item.titleView = toolbar toolbar.detailLabel.text = toolbarToText?[toolbar] ?? nil
item.titleView!.frame = bounds toolbarToText?[toolbar] = nil
}
} }
item.titleView = toolbar
item.titleView!.frame = bounds
}
} }
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