Commit 9696080d by Daniel Dahan

reduced code and drawing logic when rendering ControlViews

parent e9d23e09
...@@ -109,7 +109,7 @@ class AppSearchBarViewController: SearchBarViewController { ...@@ -109,7 +109,7 @@ class AppSearchBarViewController: SearchBarViewController {
searchBarView.tintColor = MaterialColor.grey.darken4 searchBarView.tintColor = MaterialColor.grey.darken4
searchBarView.textColor = MaterialColor.grey.darken4 searchBarView.textColor = MaterialColor.grey.darken4
searchBarView.placeholderTextColor = MaterialColor.grey.darken4 searchBarView.placeholderTextColor = MaterialColor.grey.darken4
searchBarView.textField.font = RobotoFont.regularWithSize(17) searchBarView.textField.font = RobotoFont.regular
searchBarView.textField.delegate = self searchBarView.textField.delegate = self
searchBarView.clearButton = clearButton searchBarView.clearButton = clearButton
......
...@@ -31,6 +31,11 @@ ...@@ -31,6 +31,11 @@
import UIKit import UIKit
public class ControlView : MaterialView { public class ControlView : MaterialView {
/// Will render the view.
public var willRenderView: Bool {
return 0 < width
}
/// A preset wrapper around contentInset. /// A preset wrapper around contentInset.
public var contentInsetPreset: MaterialEdgeInset { public var contentInsetPreset: MaterialEdgeInset {
get { get {
...@@ -74,14 +79,36 @@ public class ControlView : MaterialView { ...@@ -74,14 +79,36 @@ public class ControlView : MaterialView {
/// Left side UIControls. /// Left side UIControls.
public var leftControls: Array<UIControl>? { public var leftControls: Array<UIControl>? {
didSet { didSet {
reloadView() if let v: Array<UIControl> = oldValue {
for b in v {
b.removeFromSuperview()
}
}
if let v: Array<UIControl> = leftControls {
for b in v {
addSubview(b)
}
}
layoutSubviews()
} }
} }
/// Right side UIControls. /// Right side UIControls.
public var rightControls: Array<UIControl>? { public var rightControls: Array<UIControl>? {
didSet { didSet {
reloadView() if let v: Array<UIControl> = oldValue {
for b in v {
b.removeFromSuperview()
}
}
if let v: Array<UIControl> = rightControls {
for b in v {
addSubview(b)
}
}
layoutSubviews()
} }
} }
...@@ -115,24 +142,11 @@ public class ControlView : MaterialView { ...@@ -115,24 +142,11 @@ public class ControlView : MaterialView {
public override func layoutSubviews() { public override func layoutSubviews() {
super.layoutSubviews() super.layoutSubviews()
reloadView() if willRenderView {
}
public override func didMoveToSuperview() {
super.didMoveToSuperview()
reloadView()
}
/// Reloads the view.
public func reloadView() {
for v in subviews {
v.removeFromSuperview()
}
if 0 < width {
// Size of single grid column. // Size of single grid column.
if let g: CGFloat = width / CGFloat(0 < grid.axis.columns ? grid.axis.columns : 1) { if let g: CGFloat = width / CGFloat(0 < grid.axis.columns ? grid.axis.columns : 1) {
grid.views = [] grid.views = []
contentView.grid.views = []
contentView.grid.columns = grid.axis.columns contentView.grid.columns = grid.axis.columns
// leftControls // leftControls
...@@ -145,13 +159,10 @@ public class ControlView : MaterialView { ...@@ -145,13 +159,10 @@ public class ControlView : MaterialView {
c.grid.columns = 0 == g ? 1 : Int(ceil(w / g)) c.grid.columns = 0 == g ? 1 : Int(ceil(w / g))
contentView.grid.columns -= c.grid.columns contentView.grid.columns -= c.grid.columns
addSubview(c)
grid.views?.append(c) grid.views?.append(c)
} }
} }
addSubview(contentView)
grid.views?.append(contentView) grid.views?.append(contentView)
// rightControls // rightControls
...@@ -165,13 +176,11 @@ public class ControlView : MaterialView { ...@@ -165,13 +176,11 @@ public class ControlView : MaterialView {
c.grid.columns = 0 == g ? 1 : Int(ceil(w / g)) c.grid.columns = 0 == g ? 1 : Int(ceil(w / g))
contentView.grid.columns -= c.grid.columns contentView.grid.columns -= c.grid.columns
addSubview(c)
grid.views?.append(c) grid.views?.append(c)
} }
} }
grid.reloadLayout() grid.reloadLayout()
contentView.grid.reloadLayout()
} }
} }
} }
...@@ -191,6 +200,7 @@ public class ControlView : MaterialView { ...@@ -191,6 +200,7 @@ public class ControlView : MaterialView {
/// Prepares the contentView. /// Prepares the contentView.
public func prepareContentView() { public func prepareContentView() {
contentView.backgroundColor = nil contentView.backgroundColor = nil
addSubview(contentView)
} }
/** /**
......
...@@ -42,7 +42,7 @@ public class NavigationBarView : StatusBarView { ...@@ -42,7 +42,7 @@ public class NavigationBarView : StatusBarView {
if let v: UILabel = titleLabel { if let v: UILabel = titleLabel {
contentView.addSubview(v) contentView.addSubview(v)
} }
reloadView() layoutSubviews()
} }
} }
...@@ -52,7 +52,7 @@ public class NavigationBarView : StatusBarView { ...@@ -52,7 +52,7 @@ public class NavigationBarView : StatusBarView {
if let v: UILabel = detailLabel { if let v: UILabel = detailLabel {
contentView.addSubview(v) contentView.addSubview(v)
} }
reloadView() layoutSubviews()
} }
} }
...@@ -76,6 +76,8 @@ public class NavigationBarView : StatusBarView { ...@@ -76,6 +76,8 @@ public class NavigationBarView : StatusBarView {
public override func layoutSubviews() { public override func layoutSubviews() {
super.layoutSubviews() super.layoutSubviews()
if willRenderView {
// TitleView alignment. // TitleView alignment.
if let v: UILabel = titleLabel { if let v: UILabel = titleLabel {
if let d: UILabel = detailLabel { if let d: UILabel = detailLabel {
...@@ -95,30 +97,25 @@ public class NavigationBarView : StatusBarView { ...@@ -95,30 +97,25 @@ public class NavigationBarView : StatusBarView {
} }
} }
reloadView()
}
/// Prepares the contentView.
public override func prepareContentView() {
super.prepareContentView()
contentView.grid.axis.direction = .Vertical
}
/// Reloads the view.
public override func reloadView() {
super.reloadView()
if 0 < width {
contentView.grid.views = [] contentView.grid.views = []
if let v: UILabel = titleLabel { if let v: UILabel = titleLabel {
contentView.grid.views?.append(v) contentView.grid.views?.append(v)
} }
if let v: UILabel = detailLabel { if let v: UILabel = detailLabel {
contentView.grid.views?.append(v) contentView.grid.views?.append(v)
} }
contentView.grid.reloadLayout() contentView.grid.reloadLayout()
} }
} }
/// Prepares the contentView.
public override func prepareContentView() {
super.prepareContentView()
contentView.grid.axis.direction = .Vertical
}
/** /**
Used to trigger property changes that initializers avoid. Used to trigger property changes that initializers avoid.
- Parameter titleLabel: UILabel for the title. - Parameter titleLabel: UILabel for the title.
......
...@@ -164,7 +164,7 @@ public class NavigationBarViewController: StatusBarViewController { ...@@ -164,7 +164,7 @@ public class NavigationBarViewController: StatusBarViewController {
Prepares the view instance when intialized. When subclassing, Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepareView method it is recommended to override the prepareView method
to initialize property values and other setup operations. to initialize property values and other setup operations.
The super.prepareView method should always be called at the end The super.prepareView method should always be called immediately
when subclassing. when subclassing.
*/ */
public override func prepareView() { public override func prepareView() {
......
...@@ -85,24 +85,22 @@ public class SearchBarView : StatusBarView { ...@@ -85,24 +85,22 @@ public class SearchBarView : StatusBarView {
self.init(frame: CGRectZero) self.init(frame: CGRectZero)
} }
public override func reloadView() { public override func layoutSubviews() {
super.reloadView() super.layoutSubviews()
contentView.grid.views = [textField] if willRenderView {
contentView.grid.views?.append(textField)
textField.font = textField.font?.fontWithSize(20)
textField.reloadView() textField.reloadView()
} }
}
/** /// Prepares the contentView.
Prepares the view instance when intialized. When subclassing, public override func prepareContentView() {
it is recommended to override the prepareView method super.prepareContentView()
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()
prepareTextField() prepareTextField()
} }
/// Prepares the textField. /// Prepares the textField.
private func prepareTextField() { private func prepareTextField() {
textField.placeholder = "Search" textField.placeholder = "Search"
......
...@@ -86,12 +86,12 @@ public class SearchBarViewController: StatusBarViewController { ...@@ -86,12 +86,12 @@ public class SearchBarViewController: StatusBarViewController {
Prepares the view instance when intialized. When subclassing, Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepareView method it is recommended to override the prepareView method
to initialize property values and other setup operations. to initialize property values and other setup operations.
The super.prepareView method should always be called at the end The super.prepareView method should always be called immediately
when subclassing. when subclassing.
*/ */
public override func prepareView() { public override func prepareView() {
prepareSearchBarView()
super.prepareView() super.prepareView()
prepareSearchBarView()
} }
/// Prepares the SearchBarView. /// Prepares the SearchBarView.
......
...@@ -63,15 +63,15 @@ public class StatusBarView : ControlView { ...@@ -63,15 +63,15 @@ public class StatusBarView : ControlView {
} }
public override func layoutSubviews() { public override func layoutSubviews() {
super.layoutSubviews()
// Ensures a width. // Ensures a width.
if 1 > width { if !willRenderView {
width = MaterialDevice.width width = MaterialDevice.width
} }
grid.axis.columns = Int(width / 48) grid.axis.columns = Int(width / 48)
super.layoutSubviews()
// General alignment. // General alignment.
if .iPhone == MaterialDevice.type && MaterialDevice.landscape { if .iPhone == MaterialDevice.type && MaterialDevice.landscape {
grid.contentInset.top = 8 grid.contentInset.top = 8
...@@ -80,8 +80,6 @@ public class StatusBarView : ControlView { ...@@ -80,8 +80,6 @@ public class StatusBarView : ControlView {
grid.contentInset.top = heightForStatusBar + 8 grid.contentInset.top = heightForStatusBar + 8
height = heightForPortraitOrientation height = heightForPortraitOrientation
} }
reloadView()
} }
public override func intrinsicContentSize() -> CGSize { public override func intrinsicContentSize() -> CGSize {
......
...@@ -109,7 +109,7 @@ public class StatusBarViewController: UIViewController { ...@@ -109,7 +109,7 @@ public class StatusBarViewController: UIViewController {
Prepares the view instance when intialized. When subclassing, Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepareView method it is recommended to override the prepareView method
to initialize property values and other setup operations. to initialize property values and other setup operations.
The super.prepareView method should always be called at the end The super.prepareView method should always be called immediately
when subclassing. when subclassing.
*/ */
public func prepareView() { public func prepareView() {
......
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