Commit a1629384 by Daniel Dahan

updated NavigationBarView with auto handling for orientation changes

parent fedaf01a
......@@ -40,7 +40,7 @@ class ViewController: UIViewController, MaterialSwitchDelegate {
super.viewDidLoad()
prepareView()
prepareSmallMaterialSwitch()
prepareNormalMaterialSwitch()
prepareDefaultMaterialSwitch()
prepareLargeMaterialSwitch()
prepareLightOnDisabledMaterialSwitch()
prepareLightOffDisabledMaterialSwitch()
......@@ -55,76 +55,75 @@ class ViewController: UIViewController, MaterialSwitchDelegate {
/// Prepares the Small MaterialSwitch.
private func prepareSmallMaterialSwitch() {
let materialSwitch: MaterialSwitch = MaterialSwitch(state: .Off, style: .Light, size: .Small)
materialSwitch.center = view.center
materialSwitch.y -= 100
materialSwitch.delegate = self
view.addSubview(materialSwitch)
let switchControl: MaterialSwitch = MaterialSwitch(state: .Off, style: .Light, size: .Small)
switchControl.center = view.center
switchControl.y -= 100
switchControl.delegate = self
view.addSubview(switchControl)
}
/// Prepares the Normal MaterialSwitch.
private func prepareNormalMaterialSwitch() {
let materialSwitch: MaterialSwitch = MaterialSwitch(state: .On, style: .Light, size: .Normal)
materialSwitch.delegate = self
/// Prepares the Default MaterialSwitch.
private func prepareDefaultMaterialSwitch() {
let switchControl: MaterialSwitch = MaterialSwitch(state: .On, style: .Light)
switchControl.delegate = self
view.addSubview(materialSwitch)
materialSwitch.translatesAutoresizingMaskIntoConstraints = false
MaterialLayout.size(view, child: materialSwitch, width: materialSwitch.width, height: materialSwitch.height)
MaterialLayout.alignFromBottomRight(view, child: materialSwitch, bottom: 16, right: 16)
view.addSubview(switchControl)
switchControl.translatesAutoresizingMaskIntoConstraints = false
MaterialLayout.alignFromBottomRight(view, child: switchControl, bottom: 16, right: 16)
}
/// Prepares the Large MaterialSwitch.
private func prepareLargeMaterialSwitch() {
let image: UIImage? = UIImage(named: "ic_alarm_white_18pt")
let materialSwitch: MaterialSwitch = MaterialSwitch(state: .Off, style: .Light, size: .Large)
materialSwitch.center = view.center
materialSwitch.y -= 50
materialSwitch.delegate = self
materialSwitch.button.setImage(image, forState: .Normal)
materialSwitch.button.setImage(image, forState: .Highlighted)
view.addSubview(materialSwitch)
let switchControl: MaterialSwitch = MaterialSwitch(state: .Off, style: .Light, size: .Large)
switchControl.center = view.center
switchControl.y -= 50
switchControl.delegate = self
switchControl.button.setImage(image, forState: .Normal)
switchControl.button.setImage(image, forState: .Highlighted)
view.addSubview(switchControl)
}
/// Prepares the Light On enabled = false MaterialSwitch.
private func prepareLightOnDisabledMaterialSwitch() {
let materialSwitch: MaterialSwitch = MaterialSwitch(state: .On, style: .Light, size: .Normal)
materialSwitch.enabled = false
materialSwitch.center = view.center
materialSwitch.delegate = self
view.addSubview(materialSwitch)
let switchControl: MaterialSwitch = MaterialSwitch(state: .On, style: .Light)
switchControl.enabled = false
switchControl.center = view.center
switchControl.delegate = self
view.addSubview(switchControl)
}
/// Prepares the Light Off enabled = false MaterialSwitch.
private func prepareLightOffDisabledMaterialSwitch() {
let materialSwitch: MaterialSwitch = MaterialSwitch(state: .Off, style: .Light, size: .Normal)
materialSwitch.enabled = false
materialSwitch.center = view.center
materialSwitch.y += 50
materialSwitch.delegate = self
view.addSubview(materialSwitch)
let switchControl: MaterialSwitch = MaterialSwitch(state: .Off, style: .Light)
switchControl.enabled = false
switchControl.center = view.center
switchControl.y += 50
switchControl.delegate = self
view.addSubview(switchControl)
}
/// Prepares the Dark On enabled = false MaterialSwitch.
private func prepareDarkOnDisabledMaterialSwitch() {
let materialSwitch: MaterialSwitch = MaterialSwitch(state: .On, style: .Dark, size: .Normal)
materialSwitch.enabled = false
materialSwitch.center = view.center
materialSwitch.y += 100
materialSwitch.delegate = self
view.addSubview(materialSwitch)
let switchControl: MaterialSwitch = MaterialSwitch(state: .On, style: .Dark)
switchControl.enabled = false
switchControl.center = view.center
switchControl.y += 100
switchControl.delegate = self
view.addSubview(switchControl)
}
/// Prepares the Dark On enabled = false MaterialSwitch.
private func prepareDarkOffDisabledMaterialSwitch() {
let materialSwitch: MaterialSwitch = MaterialSwitch(state: .Off, style: .Dark, size: .Normal)
materialSwitch.enabled = false
materialSwitch.center = view.center
materialSwitch.y += 150
materialSwitch.delegate = self
view.addSubview(materialSwitch)
let switchControl: MaterialSwitch = MaterialSwitch(state: .Off, style: .Dark)
switchControl.enabled = false
switchControl.center = view.center
switchControl.y += 150
switchControl.delegate = self
view.addSubview(switchControl)
}
internal func materialSwitchStateChanged(control: MaterialSwitch, state: MaterialSwitchState) {
internal func switchControlStateChanged(control: MaterialSwitch, state: MaterialSwitchState) {
print("MaterialSwitch - Size: \(control.switchSize) State: \(state)")
}
}
......@@ -81,7 +81,7 @@ class ViewController: UIViewController {
searchBarView.textField.font = RobotoFont.regularWithSize(22)
searchBarView.leftControls = [menuButton]
searchBarView.rightControls = [searchButton]
// searchBarView.rightControls = [searchButton]
view.addSubview(searchBarView)
searchBarView.translatesAutoresizingMaskIntoConstraints = false
......
......@@ -69,17 +69,15 @@ Material is a growing project and will encounter changes throughout its developm
## Upcoming
* SearchBarView
* MenuViewController
* SearchBarViewController
* TabView
* TabViewController
* Scrolling Techniques
* Dialogs
* Snackbar
* ProgressBar (circular and horizontal)
* DatePicker
* TimePicker
* Switch Control
* More Examples
<a name="materialcolor"></a>
......
......@@ -297,7 +297,7 @@ public class MaterialButton : UIButton {
prepareView()
}
/// A convenience initializer that is mostly used with AutoLayout.
/// A convenience initializer.
public convenience init() {
self.init(frame: CGRectNull)
}
......@@ -406,8 +406,8 @@ public class MaterialButton : UIButton {
}
if let v: CFTimeInterval = pulseAnimation(point!) {
MaterialAnimation.delay(v) { [unowned self] in
self.shrinkAnimation()
MaterialAnimation.delay(v) { [weak self] in
self?.shrinkAnimation()
}
}
}
......@@ -483,12 +483,12 @@ public class MaterialButton : UIButton {
pulseLayer.hidden = false
}
pulseLayer.addAnimation(MaterialAnimation.scale(3 * d, duration: t), forKey: nil)
MaterialAnimation.delay(t) { [unowned self] in
if nil != self.pulseColor && 0 < self.pulseColorOpacity {
MaterialAnimation.delay(t) { [weak self] in
if nil != self && nil != self!.pulseColor && 0 < self!.pulseColorOpacity {
MaterialAnimation.animateWithDuration(t, animations: {
pulseLayer.hidden = true
}) {
pulseLayer.removeFromSuperlayer()
pulseLayer.removeFromSuperlayer()
}
}
}
......
......@@ -84,8 +84,8 @@ public class MaterialPulseView : MaterialView {
}
if let v: CFTimeInterval = pulseAnimation(point!) {
MaterialAnimation.delay(v) { [unowned self] in
self.shrinkAnimation()
MaterialAnimation.delay(v) { [weak self] in
self?.shrinkAnimation()
}
}
}
......@@ -137,12 +137,12 @@ public class MaterialPulseView : MaterialView {
pulseLayer.hidden = false
}
pulseLayer.addAnimation(MaterialAnimation.scale(3 * d, duration: t), forKey: nil)
MaterialAnimation.delay(t) { [unowned self] in
if nil != self.pulseColor && 0 < self.pulseColorOpacity {
MaterialAnimation.delay(t) { [weak self] in
if nil != self && nil != self!.pulseColor && 0 < self!.pulseColorOpacity {
MaterialAnimation.animateWithDuration(t, animations: {
pulseLayer.hidden = true
}) {
pulseLayer.removeFromSuperlayer()
pulseLayer.removeFromSuperlayer()
}
}
}
......
......@@ -42,7 +42,7 @@ public enum MaterialSwitchState {
public enum MaterialSwitchSize {
case Small
case Normal
case Default
case Large
}
......@@ -195,14 +195,14 @@ public class MaterialSwitch: UIControl {
}
/// MaterialSwitch size.
public var switchSize: MaterialSwitchSize = .Normal {
public var switchSize: MaterialSwitchSize = .Default {
didSet {
switch switchSize {
case .Small:
trackThickness = 13
buttonDiameter = 18
frame = CGRectMake(0, 0, 30, 25)
case .Normal:
case .Default:
trackThickness = 17
buttonDiameter = 24
frame = CGRectMake(0, 0, 40, 30)
......@@ -236,7 +236,7 @@ public class MaterialSwitch: UIControl {
super.init(coder: aDecoder)
prepareTrack()
prepareButton()
prepareSwitchSize(.Normal)
prepareSwitchSize(.Default)
prepareSwitchStyle(.Light)
prepareSwitchState(.Off)
}
......@@ -247,7 +247,7 @@ public class MaterialSwitch: UIControl {
- Parameter style: A MaterialSwitchStyle value.
- Parameter size: A MaterialSwitchSize value.
*/
public init(state: MaterialSwitchState = .On, style: MaterialSwitchStyle = .Light, size: MaterialSwitchSize = .Normal) {
public init(state: MaterialSwitchState = .On, style: MaterialSwitchStyle = .Light, size: MaterialSwitchSize = .Default) {
track = MaterialView(frame: CGRectZero)
button = FabButton(frame: CGRectZero)
super.init(frame: CGRectZero)
......@@ -267,7 +267,7 @@ public class MaterialSwitch: UIControl {
switch switchSize {
case .Small:
return CGSizeMake(30, 25)
case .Normal:
case .Default:
return CGSizeMake(40, 30)
case .Large:
return CGSizeMake(50, 40)
......@@ -456,7 +456,7 @@ public class MaterialSwitch: UIControl {
switch switchSize {
case .Small:
w = 30
case .Normal:
case .Default:
w = 40
case .Large:
w = 50
......
......@@ -336,7 +336,7 @@ public class MaterialView : UIView {
prepareView()
}
/// A convenience initializer that is mostly used with AutoLayout.
/// A convenience initializer.
public convenience init() {
self.init(frame: CGRectNull)
}
......
......@@ -34,74 +34,41 @@ public class NavigationBarView : MaterialView {
/// TitleView that holds the titleLabel and detailLabel.
public private(set) lazy var titleView: MaterialView = MaterialView()
/**
:name: statusBarStyle
*/
/// Device status bar style.
public var statusBarStyle: UIStatusBarStyle = UIApplication.sharedApplication().statusBarStyle {
didSet {
UIApplication.sharedApplication().statusBarStyle = statusBarStyle
}
}
/**
:name: contentInsets
*/
public var contentInsetPreset: MaterialEdgeInset = .None {
didSet {
contentInset = MaterialEdgeInsetToValue(contentInsetPreset)
}
}
/**
:name: contentInset
*/
public var contentInset: UIEdgeInsets = MaterialEdgeInsetToValue(.Square2) {
didSet {
reloadView()
}
}
/**
:name: titleLabel
*/
/// Title label.
public var titleLabel: UILabel? {
didSet {
if let v: UILabel = titleLabel {
v.grid.rows = nil == detailLabel ? 3 : 2
titleView.addSubview(v)
}
reloadView()
}
}
/**
:name: detailLabel
*/
/// Detail label.
public var detailLabel: UILabel? {
didSet {
if let v: UILabel = detailLabel {
v.grid.rows = 1
titleView.addSubview(v)
titleLabel?.grid.rows = 2
} else {
titleLabel?.grid.rows = 3
}
reloadView()
}
}
/**
:name: leftControls
*/
/// Left side UIControls.
public var leftControls: Array<UIControl>? {
didSet {
reloadView()
}
}
/**
:name: rightControls
*/
/// Right side UIControls.
public var rightControls: Array<UIControl>? {
didSet {
reloadView()
......@@ -109,36 +76,74 @@ public class NavigationBarView : MaterialView {
}
/**
:name: init
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)
}
/**
:name: init
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)
}
/**
:name: init
*/
/// A convenience initializer.
public convenience init() {
self.init(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 70))
self.init(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 64))
}
/**
:name: init
A convenience initializer with parameter settings.
- Parameter titleLabel: UILabel for the title.
- Parameter detailLabel: UILabel for the details.
- 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?(titleLabel: UILabel? = nil, detailLabel: UILabel? = nil, leftControls: Array<UIControl>? = nil, rightControls: Array<UIControl>? = nil) {
self.init(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 70))
self.init(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 64))
prepareProperties(titleLabel, detailLabel: detailLabel, leftControls: leftControls, rightControls: rightControls)
}
public override func layoutSubviews() {
super.layoutSubviews()
// General alignment.
switch UIDevice.currentDevice().orientation {
case .LandscapeLeft, .LandscapeRight:
grid.contentInset.top = 0
grid.contentInset.bottom = 0
titleView.grid.spacing = 2
titleView.grid.contentInset.top = 4
titleView.grid.contentInset.bottom = 8
height = 44
default:
grid.contentInset.top = 20
grid.contentInset.bottom = 8
titleView.grid.spacing = 0
titleView.grid.contentInset.top = 0
titleView.grid.contentInset.bottom = 0
height = 64
}
// TitleView alignment.
titleView.grid.axis.rows = 6
if let v: UILabel = titleLabel {
titleView.grid.views?.append(v)
v.grid.rows = nil == detailLabel ? 6 : 4
}
if let v: UILabel = detailLabel {
titleView.grid.views?.append(v)
v.grid.rows = 2
}
width = UIScreen.mainScreen().bounds.width
grid.axis.columns = Int(width / 48)
reloadView()
}
......@@ -147,9 +152,11 @@ public class NavigationBarView : MaterialView {
reloadView()
}
/**
:name: reloadView
*/
public override func intrinsicContentSize() -> CGSize {
return CGSizeMake(UIScreen.mainScreen().bounds.width, 64)
}
/// Reloads the view.
public func reloadView() {
layoutIfNeeded()
......@@ -162,7 +169,7 @@ public class NavigationBarView : MaterialView {
}
// Size of single grid column.
let g: CGFloat = width / CGFloat(0 < grid.columns ? grid.columns : 1)
let g: CGFloat = width / CGFloat(0 < grid.axis.columns ? grid.axis.columns : 1)
grid.views = []
titleView.grid.columns = grid.axis.columns
......@@ -206,6 +213,7 @@ public class NavigationBarView : MaterialView {
grid.reloadLayout()
titleView.grid.views = []
titleView.grid.axis.rows = 6
if let v: UILabel = titleLabel {
titleView.grid.views?.append(v)
}
......@@ -216,32 +224,38 @@ public class NavigationBarView : MaterialView {
}
/**
:name: prepareView
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()
grid.spacing = 10
grid.axis.columns = 8
grid.spacing = 8
grid.axis.inherited = false
grid.contentInset.top = 25
grid.contentInset.left = 10
grid.contentInset.bottom = 10
grid.contentInset.right = 10
grid.contentInset.top = 20
grid.contentInset.left = 8
grid.contentInset.bottom = 8
grid.contentInset.right = 8
depth = .Depth1
prepareTextView()
prepareTitleView()
}
public func prepareTextView() {
/// Prepares the titleView.
public func prepareTitleView() {
titleView.backgroundColor = nil
titleView.grid.spacing = 4
titleView.grid.axis.rows = 3
titleView.grid.axis.inherited = false
titleView.grid.axis.direction = .Vertical
addSubview(titleView)
}
/**
:name: prepareProperties
Used to trigger property changes that initializers avoid.
- Parameter titleLabel: UILabel for the title.
- Parameter detailLabel: UILabel for the details.
- 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>?) {
self.titleLabel = titleLabel
......
......@@ -362,7 +362,7 @@ public class TextField : UITextField {
prepareView()
}
/// A convenience initializer that is mostly used with AutoLayout.
/// A convenience initializer.
public convenience init() {
self.init(frame: CGRectNull)
}
......
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