Commit beff5c45 by Daniel Dahan

issue-366: added dynamic spacing to NavigationBar

parent a5d7594f
......@@ -9,6 +9,8 @@
/* Begin PBXBuildFile section */
96162C6C1CA71C8700E3A235 /* VideoViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96162C6B1CA71C8700E3A235 /* VideoViewController.swift */; };
96162C6E1CA7274E00E3A235 /* PhotoViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96162C6D1CA7274E00E3A235 /* PhotoViewController.swift */; };
961F192F1CE14C3E008927C5 /* Material.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 961F192E1CE14C3E008927C5 /* Material.framework */; };
961F19301CE14C3E008927C5 /* Material.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 961F192E1CE14C3E008927C5 /* Material.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
9663F9321C7A744600AF0965 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9663F9311C7A744600AF0965 /* AppDelegate.swift */; };
9663F9341C7A744600AF0965 /* RecommendationViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9663F9331C7A744600AF0965 /* RecommendationViewController.swift */; };
9663F9391C7A744600AF0965 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9663F9381C7A744600AF0965 /* Assets.xcassets */; };
......@@ -28,6 +30,7 @@
dstPath = "";
dstSubfolderSpec = 10;
files = (
961F19301CE14C3E008927C5 /* Material.framework in Embed Frameworks */,
);
name = "Embed Frameworks";
runOnlyForDeploymentPostprocessing = 0;
......@@ -37,6 +40,7 @@
/* Begin PBXFileReference section */
96162C6B1CA71C8700E3A235 /* VideoViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VideoViewController.swift; sourceTree = "<group>"; };
96162C6D1CA7274E00E3A235 /* PhotoViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PhotoViewController.swift; sourceTree = "<group>"; };
961F192E1CE14C3E008927C5 /* Material.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; name = Material.framework; path = "/Users/danieldahan/Library/Developer/Xcode/DerivedData/Material-bshwisidfobstlbomegqyybbrygh/Build/Products/Debug-iphoneos/Material.framework"; sourceTree = "<absolute>"; };
9663F92E1C7A744600AF0965 /* App.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = App.app; sourceTree = BUILT_PRODUCTS_DIR; };
9663F9311C7A744600AF0965 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
9663F9331C7A744600AF0965 /* RecommendationViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecommendationViewController.swift; sourceTree = "<group>"; };
......@@ -56,6 +60,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
961F192F1CE14C3E008927C5 /* Material.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
......@@ -65,6 +70,7 @@
9663F9251C7A744500AF0965 = {
isa = PBXGroup;
children = (
961F192E1CE14C3E008927C5 /* Material.framework */,
9663F9301C7A744600AF0965 /* App */,
9663F92F1C7A744600AF0965 /* Products */,
);
......
......@@ -44,7 +44,6 @@ class AppNavigationController: NavigationController {
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
statusBarView?.hidden = MaterialDevice.statusBarHidden
print(MaterialDevice.statusBarHidden)
}
/// Prepares the statusBarView
......
......@@ -37,6 +37,7 @@ public struct MaterialLayout {
public static func width(parent: UIView, child: UIView, width: CGFloat = 0, options: NSLayoutFormatOptions = []) {
let metrics: Dictionary<String, AnyObject> = ["width" : width]
let views: Dictionary<String, AnyObject> = ["child" : child]
child.translatesAutoresizingMaskIntoConstraints = false
parent.addConstraints(constraint("H:[child(width)]", options: options, metrics: metrics, views: views))
}
......@@ -46,6 +47,7 @@ public struct MaterialLayout {
public static func height(parent: UIView, child: UIView, height: CGFloat = 0, options: NSLayoutFormatOptions = []) {
let metrics: Dictionary<String, AnyObject> = ["height" : height]
let views: Dictionary<String, AnyObject> = ["child" : child]
child.translatesAutoresizingMaskIntoConstraints = false
parent.addConstraints(constraint("V:[child(height)]", options: options, metrics: metrics, views: views))
}
......@@ -53,6 +55,7 @@ public struct MaterialLayout {
:name: size
*/
public static func size(parent: UIView, child: UIView, width: CGFloat = 0, height: CGFloat = 0, options: NSLayoutFormatOptions = []) {
child.translatesAutoresizingMaskIntoConstraints = false
MaterialLayout.width(parent, child: child, width: width)
MaterialLayout.height(parent, child: child, height: height)
}
......@@ -70,6 +73,7 @@ public struct MaterialLayout {
i += 1
views[k] = v
format += i > children.count ? "[\(k)(==view1)]-(right)-|" : "[\(k)(==view1)]-(spacing)-"
v.translatesAutoresizingMaskIntoConstraints = false
}
parent.addConstraints(constraint(format, options: options, metrics: ["left" : left, "right": right, "spacing": spacing], views: views))
}
......@@ -88,6 +92,7 @@ public struct MaterialLayout {
i += 1
views[k] = v
format += i > children.count ? "[\(k)(==view1)]-(bottom)-|" : "[\(k)(==view1)]-(spacing)-"
v.translatesAutoresizingMaskIntoConstraints = false
}
parent.addConstraints(constraint(format, options: options, metrics: ["top" : top, "bottom": bottom, "spacing": spacing], views: views))
}
......@@ -97,6 +102,7 @@ public struct MaterialLayout {
:name: alignToParentHorizontally
*/
public static func alignToParentHorizontally(parent: UIView, child: UIView, left: CGFloat = 0, right: CGFloat = 0, options: NSLayoutFormatOptions = []) {
child.translatesAutoresizingMaskIntoConstraints = false
parent.addConstraints(constraint("H:|-(left)-[child]-(right)-|", options: options, metrics: ["left": left, "right": right], views: ["child" : child]))
}
......@@ -104,6 +110,7 @@ public struct MaterialLayout {
:name: alignToParentVertically
*/
public static func alignToParentVertically(parent: UIView, child: UIView, top: CGFloat = 0, bottom: CGFloat = 0, options: NSLayoutFormatOptions = []) {
child.translatesAutoresizingMaskIntoConstraints = false
parent.addConstraints(constraint("V:|-(top)-[child]-(bottom)-|", options: options, metrics: ["bottom": bottom, "top": top], views: ["child" : child]))
}
......@@ -111,6 +118,7 @@ public struct MaterialLayout {
:name: alignToParent
*/
public static func alignToParent(parent: UIView, child: UIView, top: CGFloat = 0, left: CGFloat = 0, bottom: CGFloat = 0, right: CGFloat = 0, options: NSLayoutFormatOptions = []) {
child.translatesAutoresizingMaskIntoConstraints = false
alignToParentHorizontally(parent, child: child, left: left, right: right)
alignToParentVertically(parent, child: child, top: top, bottom: bottom)
}
......@@ -119,6 +127,7 @@ public struct MaterialLayout {
:name: alignFromTopLeft
*/
public static func alignFromTopLeft(parent: UIView, child: UIView, top: CGFloat = 0, left: CGFloat = 0, options: NSLayoutFormatOptions = []) {
child.translatesAutoresizingMaskIntoConstraints = false
alignFromTop(parent, child: child, top: top)
alignFromLeft(parent, child: child, left: left)
}
......@@ -127,6 +136,7 @@ public struct MaterialLayout {
:name: alignFromTopRight
*/
public static func alignFromTopRight(parent: UIView, child: UIView, top: CGFloat = 0, right: CGFloat = 0, options: NSLayoutFormatOptions = []) {
child.translatesAutoresizingMaskIntoConstraints = false
alignFromTop(parent, child: child, top: top)
alignFromRight(parent, child: child, right: right)
}
......@@ -135,6 +145,7 @@ public struct MaterialLayout {
:name: alignFromBottomLeft
*/
public static func alignFromBottomLeft(parent: UIView, child: UIView, bottom: CGFloat = 0, left: CGFloat = 0, options: NSLayoutFormatOptions = []) {
child.translatesAutoresizingMaskIntoConstraints = false
alignFromBottom(parent, child: child, bottom: bottom)
alignFromLeft(parent, child: child, left: left)
}
......@@ -143,6 +154,7 @@ public struct MaterialLayout {
:name: alignFromBottomRight
*/
public static func alignFromBottomRight(parent: UIView, child: UIView, bottom: CGFloat = 0, right: CGFloat = 0, options: NSLayoutFormatOptions = []) {
child.translatesAutoresizingMaskIntoConstraints = false
alignFromBottom(parent, child: child, bottom: bottom)
alignFromRight(parent, child: child, right: right)
}
......@@ -151,6 +163,7 @@ public struct MaterialLayout {
:name: alignFromTop
*/
public static func alignFromTop(parent: UIView, child: UIView, top: CGFloat = 0, options: NSLayoutFormatOptions = []) {
child.translatesAutoresizingMaskIntoConstraints = false
parent.addConstraints(constraint("V:|-(top)-[child]", options: options, metrics: ["top" : top], views: ["child" : child]))
}
......@@ -158,6 +171,7 @@ public struct MaterialLayout {
:name: alignFromLeft
*/
public static func alignFromLeft(parent: UIView, child: UIView, left: CGFloat = 0, options: NSLayoutFormatOptions = []) {
child.translatesAutoresizingMaskIntoConstraints = false
parent.addConstraints(constraint("H:|-(left)-[child]", options: options, metrics: ["left" : left], views: ["child" : child]))
}
......@@ -165,6 +179,7 @@ public struct MaterialLayout {
:name: alignFromBottom
*/
public static func alignFromBottom(parent: UIView, child: UIView, bottom: CGFloat = 0, options: NSLayoutFormatOptions = []) {
child.translatesAutoresizingMaskIntoConstraints = false
parent.addConstraints(constraint("V:[child]-(bottom)-|", options: options, metrics: ["bottom" : bottom], views: ["child" : child]))
}
......@@ -172,6 +187,7 @@ public struct MaterialLayout {
:name: alignFromRight
*/
public static func alignFromRight(parent: UIView, child: UIView, right: CGFloat = 0, options: NSLayoutFormatOptions = []) {
child.translatesAutoresizingMaskIntoConstraints = false
parent.addConstraints(constraint("H:[child]-(right)-|", options: options, metrics: ["right" : right], views: ["child" : child]))
}
......
......@@ -44,6 +44,12 @@ public extension UINavigationBar {
@IBDesignable
public class NavigationBar : UINavigationBar {
/// The current layout.
private var isLandscape: Bool = false
/// the spacer inset value.
private var spacerInset: CGFloat = 0
/// Left spacer moves the items to the left edge of the NavigationBar.
private var leftSpacer: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .FixedSpace, target: nil, action: nil)
......@@ -261,14 +267,31 @@ public class NavigationBar : UINavigationBar {
public override func layoutSubviews() {
super.layoutSubviews()
if !isLandscape && MaterialDevice.isLandscape {
isLandscape = true
spacerInset = 0
} else if isLandscape && MaterialDevice.isPortrait {
isLandscape = false
spacerInset = 0
}
if let v: UINavigationItem = topItem {
if 0 == spacerInset {
layoutNavigationItem(v)
} else {
sizeNavigationItem(v)
}
}
if let v: UINavigationItem = backItem {
if 0 == spacerInset {
layoutNavigationItem(v)
} else {
sizeNavigationItem(v)
}
}
}
public override func pushNavigationItem(item: UINavigationItem, animated: Bool) {
super.pushNavigationItem(item, animated: animated)
......@@ -284,19 +307,23 @@ public class NavigationBar : UINavigationBar {
// leftControls
if let v: Array<UIControl> = item.leftControls {
if 0 < v.count {
var n: Array<UIBarButtonItem> = Array<UIBarButtonItem>()
for c in v {
n.append(UIBarButtonItem(customView: c))
}
n.append(leftSpacer)
item.leftBarButtonItems = n.reverse()
if 0 == spacerInset {
spacerInset = n.first!.customView!.frame.origin.x
}
}
}
// Set the titleView if title is empty.
if "" == item.title {
if nil == item.titleView {
item.titleView = UIView(frame: CGRectMake(0, contentInset.top, MaterialDevice.width < MaterialDevice.height ? MaterialDevice.height : MaterialDevice.width, intrinsicContentSize().height - contentInset.top - contentInset.bottom))
item.titleView!.autoresizingMask = [.FlexibleWidth]
item.titleView!.grid.axis.direction = .Vertical
}
......@@ -321,6 +348,7 @@ public class NavigationBar : UINavigationBar {
// rightControls
if let v: Array<UIControl> = item.rightControls {
if 0 < v.count {
var n: Array<UIBarButtonItem> = Array<UIBarButtonItem>()
for c in v {
n.append(UIBarButtonItem(customView: c))
......@@ -328,8 +356,11 @@ public class NavigationBar : UINavigationBar {
n.append(rightSpacer)
item.rightBarButtonItems = n.reverse()
if 0 == spacerInset {
spacerInset = width - n[n.count - 2].customView!.frame.origin.x - n[n.count - 2].customView!.frame.width
}
}
}
sizeNavigationItem(item)
}
......@@ -338,9 +369,11 @@ public class NavigationBar : UINavigationBar {
- Parameter item: A UINavigationItem to size.
*/
internal func sizeNavigationItem(item: UINavigationItem) {
print(spacerInset)
let h: CGFloat = intrinsicContentSize().height
let w: CGFloat = backButton.intrinsicContentSize().width
let inset: CGFloat = MaterialDevice.isLandscape ? item.landscapeInset : item.portraitInset
var spaceLeft: CGFloat = 0
var spaceRight: CGFloat = 0
var capturedSpace: Bool = false
// leftControls
if let v: Array<UIControl> = item.leftControls {
......@@ -349,9 +382,12 @@ public class NavigationBar : UINavigationBar {
b.contentEdgeInsets.top = 0
b.contentEdgeInsets.bottom = 0
}
c.bounds.size = c is MaterialSwitch ? CGSizeMake(w, h - contentInset.top - contentInset.bottom) : CGSizeMake(c.intrinsicContentSize().width, h - contentInset.top - contentInset.bottom)
c.bounds.size = CGSizeMake(c.intrinsicContentSize().width, h - contentInset.top - contentInset.bottom)
c.backgroundColor = MaterialColor.purple.base
spaceLeft += c.bounds.size.width + contentInset.left
}
leftSpacer.width = inset + contentInset.left
leftSpacer.width = contentInset.left - spacerInset
spaceLeft += contentInset.left
}
item.titleView?.frame.size.width = width
......@@ -382,10 +418,18 @@ public class NavigationBar : UINavigationBar {
b.contentEdgeInsets.top = 0
b.contentEdgeInsets.bottom = 0
}
c.bounds.size = c is MaterialSwitch ? CGSizeMake(w, h - contentInset.top - contentInset.bottom) : CGSizeMake(c.intrinsicContentSize().width, h - contentInset.top - contentInset.bottom)
c.bounds.size = CGSizeMake(c.intrinsicContentSize().width, h - contentInset.top - contentInset.bottom)
c.backgroundColor = MaterialColor.purple.base.colorWithAlphaComponent(0.1)
spaceRight += c.bounds.size.width + contentInset.right
}
rightSpacer.width = inset + contentInset.right
rightSpacer.width = contentInset.right - spacerInset
spaceRight += contentInset.right
}
item.titleView?.backgroundColor = MaterialColor.green.base
item.titleView?.frame.size.width = width - spaceLeft - spaceRight
item.titleView?.frame.origin.x = spaceLeft
item.titleView?.grid.reloadLayout()
}
/**
......@@ -396,6 +440,7 @@ public class NavigationBar : UINavigationBar {
when subclassing.
*/
public func prepareView() {
isLandscape = MaterialDevice.isLandscape
barStyle = .Default
translucent = false
backButtonImage = nil
......
......@@ -34,12 +34,6 @@ import UIKit
private var MaterialAssociatedObjectNavigationItemKey: UInt8 = 0
public class MaterialAssociatedObjectNavigationItem {
/// Portrait inset.
public var portraitInset: CGFloat
/// Landscape inset.
public var landscapeInset: CGFloat
/// Detail View.
public var detailView: UIView?
......@@ -54,11 +48,6 @@ public class MaterialAssociatedObjectNavigationItem {
/// Right controls.
public var rightControls: Array<UIControl>?
public init(portraitInset: CGFloat, landscapeInset: CGFloat) {
self.portraitInset = portraitInset
self.landscapeInset = landscapeInset
}
}
public extension UINavigationItem {
......@@ -66,7 +55,7 @@ public extension UINavigationItem {
public internal(set) var item: MaterialAssociatedObjectNavigationItem {
get {
return MaterialAssociatedObject(self, key: &MaterialAssociatedObjectNavigationItemKey) {
return MaterialAssociatedObjectNavigationItem(portraitInset: .iPad == MaterialDevice.type || "iPhone 6s Plus" == MaterialDevice.model || "iPhone 6 Plus" == MaterialDevice.model ? -20 : -16, landscapeInset: -20)
return MaterialAssociatedObjectNavigationItem()
}
}
set(value) {
......@@ -74,26 +63,6 @@ public extension UINavigationItem {
}
}
/// Portrait inset.
public var portraitInset: CGFloat {
get {
return item.portraitInset
}
set(value) {
item.portraitInset = value
}
}
/// Landscape inset.
public var landscapeInset: CGFloat {
get {
return item.landscapeInset
}
set(value) {
item.landscapeInset = value
}
}
/// Detail View.
public var detailView: UIView? {
get {
......
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