Commit 5e4b073b by Daniel Dahan

refactored NavigationBarView to use MaterialControlView

parent c5569078
......@@ -43,6 +43,7 @@
96A71EF61C71127100C0C4AE /* SearchBarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96A71EF51C71127100C0C4AE /* SearchBarView.swift */; };
96A71F301C72E41100C0C4AE /* NavigationViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96A71F2F1C72E41100C0C4AE /* NavigationViewController.swift */; };
96A71F451C74ABCB00C0C4AE /* MaterialBorder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96A71F441C74ABCB00C0C4AE /* MaterialBorder.swift */; };
96A71FB11C7649F800C0C4AE /* MaterialControlView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96A71FB01C7649F800C0C4AE /* MaterialControlView.swift */; };
96D88C1E1C1328D800B91418 /* CaptureView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96D88BF51C1328D800B91418 /* CaptureView.swift */; };
96D88C1F1C1328D800B91418 /* CardView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96D88BF61C1328D800B91418 /* CardView.swift */; };
96D88C201C1328D800B91418 /* CapturePreviewView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96D88BF71C1328D800B91418 /* CapturePreviewView.swift */; };
......@@ -145,6 +146,7 @@
96A71EF51C71127100C0C4AE /* SearchBarView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SearchBarView.swift; sourceTree = "<group>"; };
96A71F2F1C72E41100C0C4AE /* NavigationViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NavigationViewController.swift; sourceTree = "<group>"; };
96A71F441C74ABCB00C0C4AE /* MaterialBorder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MaterialBorder.swift; sourceTree = "<group>"; };
96A71FB01C7649F800C0C4AE /* MaterialControlView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MaterialControlView.swift; sourceTree = "<group>"; };
96D88BF51C1328D800B91418 /* CaptureView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CaptureView.swift; sourceTree = "<group>"; };
96D88BF61C1328D800B91418 /* CardView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CardView.swift; sourceTree = "<group>"; };
96D88BF71C1328D800B91418 /* CapturePreviewView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CapturePreviewView.swift; sourceTree = "<group>"; };
......@@ -434,6 +436,7 @@
children = (
96D88C141C1328D800B91418 /* MaterialView.swift */,
96D88C0D1C1328D800B91418 /* MaterialPulseView.swift */,
96A71FB01C7649F800C0C4AE /* MaterialControlView.swift */,
);
name = View;
sourceTree = "<group>";
......@@ -624,6 +627,7 @@
96D88C371C1328D800B91418 /* MaterialRadius.swift in Sources */,
960B232F1C383EAA00E96216 /* Material+UIImage+Crop.swift in Sources */,
96D88C241C1328D800B91418 /* ImageCardView.swift in Sources */,
96A71FB11C7649F800C0C4AE /* MaterialControlView.swift in Sources */,
96D88C2B1C1328D800B91418 /* MaterialButton.swift in Sources */,
96D88C311C1328D800B91418 /* MaterialKeyframeAnimation.swift in Sources */,
96D88C2E1C1328D800B91418 /* MaterialFont.swift in Sources */,
......
/*
* Copyright (C) 2015 - 2016, Daniel Dahan and CosmicMind, Inc. <http://cosmicmind.io>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Material nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import UIKit
public class MaterialControlView : MaterialView {
/// ContentView that holds the any desired subviews.
public private(set) lazy var contentView: MaterialView = MaterialView()
/// A preset wrapper around contentInset.
public var contentInsetPreset: MaterialEdgeInset = .None {
didSet {
contentInset = MaterialEdgeInsetToValue(contentInsetPreset)
}
}
/// A wrapper around grid.contentInset.
public var contentInset: UIEdgeInsets {
get {
return grid.contentInset
}
set(value) {
grid.contentInset = contentInset
reloadView()
}
}
/// A wrapper around grid.spacing.
public var spacing: CGFloat {
get {
return grid.spacing
}
set(value) {
grid.spacing = spacing
reloadView()
}
}
/// Left side UIControls.
public var leftControls: Array<UIControl>? {
didSet {
reloadView()
}
}
/// Right side UIControls.
public var rightControls: Array<UIControl>? {
didSet {
reloadView()
}
}
/**
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)
}
/**
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)
}
/**
A convenience initializer with parameter settings.
- Parameter leftControls: An Array of UIControls that go on the left side.
- Parameter rightControls: An Array of UIControls that go on the right side.
*/
public convenience init?(leftControls: Array<UIControl>? = nil, rightControls: Array<UIControl>? = nil) {
self.init(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 64))
prepareProperties(leftControls: leftControls, rightControls: rightControls)
}
public override func layoutSubviews() {
super.layoutSubviews()
reloadView()
}
public override func didMoveToSuperview() {
super.didMoveToSuperview()
reloadView()
}
/// Reloads the view.
public func reloadView() {
layoutIfNeeded()
// clear constraints so new ones do not conflict
removeConstraints(constraints)
for v in subviews {
if v != contentView {
v.removeFromSuperview()
}
}
// Size of single grid column.
let g: CGFloat = width / CGFloat(0 < grid.axis.columns ? grid.axis.columns : 1)
grid.views = []
contentView.grid.columns = grid.axis.columns
// leftControls
if let v: Array<UIControl> = leftControls {
for c in v {
let w: CGFloat = c.intrinsicContentSize().width
if let b: UIButton = c as? UIButton {
b.contentEdgeInsets = UIEdgeInsetsZero
}
c.grid.columns = 0 == g ? 1 : Int(ceil(w / g))
contentView.grid.columns -= c.grid.columns
addSubview(c)
grid.views?.append(c)
}
}
grid.views?.append(contentView)
// rightControls
if let v: Array<UIControl> = rightControls {
for c in v {
let w: CGFloat = c.intrinsicContentSize().width
if let b: UIButton = c as? UIButton {
b.contentEdgeInsets = UIEdgeInsetsZero
}
c.grid.columns = 0 == g ? 1 : Int(ceil(w / g))
contentView.grid.columns -= c.grid.columns
addSubview(c)
grid.views?.append(c)
}
}
contentView.grid.columns -= contentView.grid.offset.columns
grid.reloadLayout()
}
/**
Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepareView method
to initialize property values and other setup operations.
The super.prepareView method should always be called immediately
when subclassing.
*/
public override func prepareView() {
super.prepareView()
prepareContentView()
}
/// Prepares the contentView.
public func prepareContentView() {
contentView.backgroundColor = nil
addSubview(contentView)
}
/**
Used to trigger property changes that initializers avoid.
- Parameter leftControls: An Array of UIControls that go on the left side.
- Parameter rightControls: An Array of UIControls that go on the right side.
*/
internal func prepareProperties(leftControls leftControls: Array<UIControl>?, rightControls: Array<UIControl>?) {
self.leftControls = leftControls
self.rightControls = rightControls
}
}
......@@ -35,13 +35,10 @@ public protocol NavigationBarViewDelegate : MaterialDelegate {
optional func navigationBarViewDidChangeLayout(navigationBarView: NavigationBarView)
}
public class NavigationBarView : MaterialView {
public class NavigationBarView : MaterialControlView {
/// Tracks the old frame size.
private var oldFrame: CGRect?
/// TitleView that holds the titleLabel and detailLabel.
public private(set) lazy var titleView: MaterialView = MaterialView()
/// Device status bar style.
public var statusBarStyle: UIStatusBarStyle = UIApplication.sharedApplication().statusBarStyle {
didSet {
......@@ -49,40 +46,11 @@ public class NavigationBarView : MaterialView {
}
}
/// A preset wrapper around contentInset.
public var contentInsetPreset: MaterialEdgeInset = .None {
didSet {
contentInset = MaterialEdgeInsetToValue(contentInsetPreset)
}
}
/// A wrapper around grid.contentInset.
public var contentInset: UIEdgeInsets {
get {
return grid.contentInset
}
set(value) {
grid.contentInset = contentInset
reloadView()
}
}
/// A wrapper around grid.spacing.
public var spacing: CGFloat {
get {
return grid.spacing
}
set(value) {
grid.spacing = spacing
reloadView()
}
}
/// Title label.
public var titleLabel: UILabel? {
didSet {
if let v: UILabel = titleLabel {
titleView.addSubview(v)
contentView.addSubview(v)
}
reloadView()
}
......@@ -92,44 +60,12 @@ public class NavigationBarView : MaterialView {
public var detailLabel: UILabel? {
didSet {
if let v: UILabel = detailLabel {
titleView.addSubview(v)
contentView.addSubview(v)
}
reloadView()
}
}
/// Left side UIControls.
public var leftControls: Array<UIControl>? {
didSet {
reloadView()
}
}
/// Right side UIControls.
public var rightControls: Array<UIControl>? {
didSet {
reloadView()
}
}
/**
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)
}
/**
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)
}
/// A convenience initializer.
public convenience init() {
self.init(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 64))
......@@ -144,7 +80,7 @@ public class NavigationBarView : MaterialView {
*/
public convenience init?(titleLabel: UILabel? = nil, detailLabel: UILabel? = nil, leftControls: Array<UIControl>? = nil, rightControls: Array<UIControl>? = nil) {
self.init(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 64))
prepareProperties(titleLabel, detailLabel: detailLabel, leftControls: leftControls, rightControls: rightControls)
prepareProperties(titleLabel: titleLabel, detailLabel: detailLabel, leftControls: leftControls, rightControls: rightControls)
}
public override func layoutSubviews() {
......@@ -159,12 +95,12 @@ public class NavigationBarView : MaterialView {
if let d: UILabel = detailLabel {
v.grid.rows = 7
d.grid.rows = 5
titleView.grid.spacing = 4
titleView.grid.contentInset.top = -3
contentView.grid.spacing = 4
contentView.grid.contentInset.top = -3
} else {
v.grid.rows = 12
titleView.grid.spacing = 0
titleView.grid.contentInset.top = 0
contentView.grid.spacing = 0
contentView.grid.contentInset.top = 0
}
}
height = 44
......@@ -176,12 +112,12 @@ public class NavigationBarView : MaterialView {
if let d: UILabel = detailLabel {
v.grid.rows = 7
d.grid.rows = 5
titleView.grid.spacing = 4
titleView.grid.contentInset.top = -3
contentView.grid.spacing = 4
contentView.grid.contentInset.top = -3
} else {
v.grid.rows = 12
titleView.grid.spacing = 0
titleView.grid.contentInset.top = 0
contentView.grid.spacing = 0
contentView.grid.contentInset.top = 0
}
}
height = 64
......@@ -211,70 +147,18 @@ public class NavigationBarView : MaterialView {
}
/// Reloads the view.
public func reloadView() {
layoutIfNeeded()
// clear constraints so new ones do not conflict
removeConstraints(constraints)
for v in subviews {
if v != titleView {
v.removeFromSuperview()
}
}
// Size of single grid column.
let g: CGFloat = width / CGFloat(0 < grid.axis.columns ? grid.axis.columns : 1)
grid.views = []
titleView.grid.columns = grid.axis.columns
// leftControls
if let v: Array<UIControl> = leftControls {
for c in v {
let w: CGFloat = c.intrinsicContentSize().width
if let b: UIButton = c as? UIButton {
b.contentEdgeInsets = UIEdgeInsetsZero
}
c.grid.columns = 0 == g ? 1 : Int(ceil(w / g))
titleView.grid.columns -= c.grid.columns
addSubview(c)
grid.views?.append(c)
}
}
grid.views?.append(titleView)
// rightControls
if let v: Array<UIControl> = rightControls {
for c in v {
let w: CGFloat = c.intrinsicContentSize().width
if let b: UIButton = c as? UIButton {
b.contentEdgeInsets = UIEdgeInsetsZero
}
c.grid.columns = 0 == g ? 1 : Int(ceil(w / g))
titleView.grid.columns -= c.grid.columns
addSubview(c)
grid.views?.append(c)
}
}
titleView.grid.columns -= titleView.grid.offset.columns
grid.reloadLayout()
titleView.grid.views = []
titleView.grid.axis.rows = 6
public override func reloadView() {
super.reloadView()
contentView.grid.views = []
contentView.grid.axis.rows = 6
if let v: UILabel = titleLabel {
titleView.grid.views?.append(v)
contentView.grid.views?.append(v)
}
if let v: UILabel = detailLabel {
titleView.grid.views?.append(v)
contentView.grid.views?.append(v)
}
titleView.grid.reloadLayout()
contentView.grid.reloadLayout()
}
/**
......@@ -287,20 +171,13 @@ public class NavigationBarView : MaterialView {
public override func prepareView() {
super.prepareView()
oldFrame = frame
depth = .Depth1
grid.spacing = 8
grid.axis.inherited = false
grid.contentInset.left = 8
grid.contentInset.bottom = 8
grid.contentInset.right = 8
depth = .Depth1
prepareTitleView()
}
/// Prepares the titleView.
public func prepareTitleView() {
titleView.backgroundColor = nil
titleView.grid.axis.direction = .Vertical
addSubview(titleView)
grid.axis.inherited = false
contentView.grid.axis.inherited = false
}
/**
......@@ -310,10 +187,10 @@ public class NavigationBarView : MaterialView {
- Parameter leftControls: An Array of UIControls that go on the left side.
- Parameter rightControls: An Array of UIControls that go on the right side.
*/
internal func prepareProperties(titleLabel: UILabel?, detailLabel: UILabel?, leftControls: Array<UIControl>?, rightControls: Array<UIControl>?) {
internal func prepareProperties(titleLabel titleLabel: UILabel?, detailLabel: UILabel?, leftControls: Array<UIControl>?, rightControls: Array<UIControl>?) {
prepareProperties(leftControls: leftControls, rightControls: rightControls)
self.titleLabel = titleLabel
self.detailLabel = detailLabel
self.leftControls = leftControls
self.rightControls = 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