Commit 17733f51 by intoxicated

Fixed iOS 11 navigationBar layoutMargin to zero

parent 0eb7acf7
......@@ -31,177 +31,183 @@
import UIKit
open class NavigationBar: UINavigationBar {
/// Will layout the view.
open var willLayout: Bool {
return 0 < bounds.width && 0 < bounds.height && nil != superview
/// Will layout the view.
open var willLayout: Bool {
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?]?
open override var intrinsicContentSize: CGSize {
return CGSize(width: bounds.width, height: bounds.height)
}
/// A reference to EdgeInsets.
@IBInspectable
open var contentEdgeInsets = EdgeInsets.zero {
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.
open var contentEdgeInsetsPreset = EdgeInsetsPreset.none {
didSet {
contentEdgeInsets = EdgeInsetsPresetToValue(preset: contentEdgeInsetsPreset)
}
//iOS 11 added left/right layout margin in subviews of UINavigationBar
//since we do not want to unsafely access private view directly
//iterate subviews to set `layoutMargin` to zero
for view in subviews {
view.layoutMargins = .zero
}
/// A reference to EdgeInsets.
@IBInspectable
open var contentEdgeInsets = EdgeInsets.zero {
didSet {
layoutSubviews()
}
if let v = topItem {
layoutNavigationItem(item: v)
}
/// A preset wrapper around interimSpace.
open var interimSpacePreset = InterimSpacePreset.interimSpace3 {
didSet {
interimSpace = InterimSpacePresetToValue(preset: interimSpacePreset)
}
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 = .none
contentScaleFactor = Screen.scale
contentEdgeInsetsPreset = .none
interimSpacePreset = .none
backButtonImage = Icon.cm.arrowBack
/// A wrapper around grid.interimSpace.
@IBInspectable
open var interimSpace: InterimSpace = 0 {
didSet {
layoutSubviews()
}
if #available(iOS 11, *) {
toolbarToText = [:]
}
/**
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()
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
}
let image = UIImage()
shadowImage = image
setBackgroundImage(image, for: .default)
backgroundColor = .white
}
}
internal extension NavigationBar {
/**
Lays out the UINavigationItem.
- Parameter item: A UINavigationItem to layout.
*/
func layoutNavigationItem(item: UINavigationItem) {
guard willLayout else {
return
}
let toolbar = item.toolbar
toolbar.backgroundColor = .clear
toolbar.interimSpace = interimSpace
toolbar.contentEdgeInsets = contentEdgeInsets
/**
Lays out the UINavigationItem.
- Parameter item: A UINavigationItem to layout.
*/
func layoutNavigationItem(item: UINavigationItem) {
guard willLayout else {
return
}
let toolbar = item.toolbar
toolbar.backgroundColor = .clear
toolbar.interimSpace = interimSpace
toolbar.contentEdgeInsets = contentEdgeInsets
if #available(iOS 11, *) {
if Application.shouldStatusBarBeHidden {
toolbar.contentEdgeInsetsPreset = .none
if #available(iOS 11, *) {
if Application.shouldStatusBarBeHidden {
toolbar.contentEdgeInsetsPreset = .none
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
}
if nil != toolbar.detailLabel.text {
toolbarToText?[toolbar] = toolbar.detailLabel.text
toolbar.detailLabel.text = nil
}
item.titleView = toolbar
item.titleView!.frame = bounds
} else if nil != toolbarToText?[toolbar] {
toolbar.detailLabel.text = toolbarToText?[toolbar] ?? nil
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