Commit e147eb9a by Daniel Dahan

storyboard-examples: updated Switch and fixed an issue where Switch in a Bar or…

storyboard-examples: updated Switch and fixed an issue where Switch in a Bar or NavigationBar would behave incorrectly, issue-540
parent 1fc740e4
...@@ -36,6 +36,7 @@ class RootViewController: UIViewController { ...@@ -36,6 +36,7 @@ class RootViewController: UIViewController {
private var menuButton: IconButton! private var menuButton: IconButton!
private var starButton: IconButton! private var starButton: IconButton!
private var searchButton: IconButton! private var searchButton: IconButton!
private var switchControl: Switch!
/// Trigger to go to the next view controller. /// Trigger to go to the next view controller.
private var nextButton: FlatButton! private var nextButton: FlatButton!
...@@ -77,7 +78,9 @@ class RootViewController: UIViewController { ...@@ -77,7 +78,9 @@ class RootViewController: UIViewController {
navigationItem.detail = "Build Beautiful Software" navigationItem.detail = "Build Beautiful Software"
navigationItem.detailLabel.textColor = Color.lightBlue.lighten5 navigationItem.detailLabel.textColor = Color.lightBlue.lighten5
navigationItem.leftViews = [menuButton] switchControl = Switch()
navigationItem.leftViews = [switchControl, menuButton]
navigationItem.rightViews = [starButton, searchButton] navigationItem.rightViews = [starButton, searchButton]
} }
......
...@@ -36,7 +36,6 @@ class RootViewController: UIViewController { ...@@ -36,7 +36,6 @@ class RootViewController: UIViewController {
private var menuButton: IconButton! private var menuButton: IconButton!
private var starButton: IconButton! private var starButton: IconButton!
private var searchButton: IconButton! private var searchButton: IconButton!
private var switchControl: Switch!
open override func viewDidLoad() { open override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
...@@ -75,10 +74,8 @@ class RootViewController: UIViewController { ...@@ -75,10 +74,8 @@ class RootViewController: UIViewController {
tc.toolbar.detail = "Build Beautiful Software" tc.toolbar.detail = "Build Beautiful Software"
tc.toolbar.detailLabel.textAlignment = .left tc.toolbar.detailLabel.textAlignment = .left
switchControl = Switch(state: .off, style:.light, size: .small)
tc.toolbar.leftViews = [menuButton] tc.toolbar.leftViews = [menuButton]
tc.toolbar.rightViews = [switchControl, starButton, searchButton] tc.toolbar.rightViews = [starButton, searchButton]
} }
} }
...@@ -191,6 +191,7 @@ open class Bar: View { ...@@ -191,6 +191,7 @@ open class Bar: View {
for v in leftViews { for v in leftViews {
(v as? UIButton)?.contentEdgeInsets = .zero (v as? UIButton)?.contentEdgeInsets = .zero
v.width = v.intrinsicContentSize.width
v.sizeToFit() v.sizeToFit()
v.grid.columns = Int(ceil(v.width / gridFactor)) + 1 v.grid.columns = Int(ceil(v.width / gridFactor)) + 1
...@@ -203,6 +204,7 @@ open class Bar: View { ...@@ -203,6 +204,7 @@ open class Bar: View {
for v in rightViews { for v in rightViews {
(v as? UIButton)?.contentEdgeInsets = .zero (v as? UIButton)?.contentEdgeInsets = .zero
v.width = v.intrinsicContentSize.width
v.sizeToFit() v.sizeToFit()
v.grid.columns = Int(ceil(v.width / gridFactor)) + 1 v.grid.columns = Int(ceil(v.width / gridFactor)) + 1
......
...@@ -209,6 +209,7 @@ open class NavigationBar: UINavigationBar { ...@@ -209,6 +209,7 @@ open class NavigationBar: UINavigationBar {
for v in item.leftViews { for v in item.leftViews {
(v as? UIButton)?.contentEdgeInsets = .zero (v as? UIButton)?.contentEdgeInsets = .zero
v.width = v.intrinsicContentSize.width
v.sizeToFit() v.sizeToFit()
v.grid.columns = Int(ceil(v.width / gridFactor)) + 1 v.grid.columns = Int(ceil(v.width / gridFactor)) + 1
...@@ -221,6 +222,7 @@ open class NavigationBar: UINavigationBar { ...@@ -221,6 +222,7 @@ open class NavigationBar: UINavigationBar {
for v in item.rightViews { for v in item.rightViews {
(v as? UIButton)?.contentEdgeInsets = .zero (v as? UIButton)?.contentEdgeInsets = .zero
v.width = v.intrinsicContentSize.width
v.sizeToFit() v.sizeToFit()
v.grid.columns = Int(ceil(v.width / gridFactor)) + 1 v.grid.columns = Int(ceil(v.width / gridFactor)) + 1
......
...@@ -238,18 +238,8 @@ open class Switch: UIControl { ...@@ -238,18 +238,8 @@ open class Switch: UIControl {
trackThickness = 24 trackThickness = 24
buttonDiameter = 32 buttonDiameter = 32
} }
}
}
open override var frame: CGRect { frame.size = intrinsicContentSize
didSet {
reload()
}
}
open override var bounds: CGRect {
didSet {
reload()
} }
} }
...@@ -300,9 +290,14 @@ open class Switch: UIControl { ...@@ -300,9 +290,14 @@ open class Switch: UIControl {
button = FabButton() button = FabButton()
super.init(frame: .zero) super.init(frame: .zero)
prepare() prepare()
prepareSwitchSize(size: size)
prepareSwitchStyle(style: style)
prepareSwitchState(state: state) prepareSwitchState(state: state)
prepareSwitchStyle(style: style)
prepareSwitchSize(size: size)
}
open override func layoutSubviews() {
super.layoutSubviews()
reload()
} }
open override func willMove(toSuperview newSuperview: UIView?) { open override func willMove(toSuperview newSuperview: UIView?) {
...@@ -339,14 +334,17 @@ open class Switch: UIControl { ...@@ -339,14 +334,17 @@ open class Switch: UIControl {
} }
internalSwitchState = state internalSwitchState = state
if animated { if animated {
animateToState(state: state) { [weak self] _ in animateToState(state: state) { [weak self] _ in
if let s: Switch = self { guard let s = self else {
return
}
s.sendActions(for: .valueChanged) s.sendActions(for: .valueChanged)
completion?(s) completion?(s)
s.delegate?.switchDidChangeState(control: s, state: s.internalSwitchState) s.delegate?.switchDidChangeState(control: s, state: s.internalSwitchState)
} }
}
} else { } else {
button.x = .on == state ? self.onPosition : self.offPosition button.x = .on == state ? self.onPosition : self.offPosition
styleForState(state: state) styleForState(state: state)
...@@ -415,11 +413,12 @@ open class Switch: UIControl { ...@@ -415,11 +413,12 @@ open class Switch: UIControl {
when subclassing. when subclassing.
*/ */
open func prepare() { open func prepare() {
contentScaleFactor = Device.scale
prepareTrack() prepareTrack()
prepareButton() prepareButton()
prepareSwitchSize(size: .medium) prepareSwitchState()
prepareSwitchStyle(style: .light) prepareSwitchStyle()
prepareSwitchState(state: .off) prepareSwitchSize()
} }
/// Laout the button and track views. /// Laout the button and track views.
...@@ -459,7 +458,7 @@ open class Switch: UIControl { ...@@ -459,7 +458,7 @@ open class Switch: UIControl {
init to set the state value and have an effect. init to set the state value and have an effect.
- Parameter state: The SwitchState to set. - Parameter state: The SwitchState to set.
*/ */
private func prepareSwitchState(state: SwitchState) { private func prepareSwitchState(state: SwitchState = .off) {
setSwitchState(state: state, animated: false) setSwitchState(state: state, animated: false)
} }
...@@ -468,7 +467,7 @@ open class Switch: UIControl { ...@@ -468,7 +467,7 @@ open class Switch: UIControl {
init to set the state value and have an effect. init to set the state value and have an effect.
- Parameter style: The SwitchStyle to set. - Parameter style: The SwitchStyle to set.
*/ */
private func prepareSwitchStyle(style: SwitchStyle) { private func prepareSwitchStyle(style: SwitchStyle = .light) {
switchStyle = style switchStyle = style
} }
...@@ -477,7 +476,7 @@ open class Switch: UIControl { ...@@ -477,7 +476,7 @@ open class Switch: UIControl {
init to set the size value and have an effect. init to set the size value and have an effect.
- Parameter size: The SwitchSize to set. - Parameter size: The SwitchSize to set.
*/ */
private func prepareSwitchSize(size: SwitchSize) { private func prepareSwitchSize(size: SwitchSize = .medium) {
switchSize = size switchSize = size
} }
...@@ -532,22 +531,28 @@ open class Switch: UIControl { ...@@ -532,22 +531,28 @@ open class Switch: UIControl {
delay: 0.05, delay: 0.05,
options: [.curveEaseIn, .curveEaseOut], options: [.curveEaseIn, .curveEaseOut],
animations: { [weak self] in animations: { [weak self] in
if let s: Switch = self { guard let s = self else {
return
}
s.button.x = .on == state ? s.onPosition + s.bounceOffset : s.offPosition - s.bounceOffset s.button.x = .on == state ? s.onPosition + s.bounceOffset : s.offPosition - s.bounceOffset
s.styleForState(state: state) s.styleForState(state: state)
}
}) { [weak self] _ in }) { [weak self] _ in
UIView.animate(withDuration: 0.15, UIView.animate(withDuration: 0.15,
animations: { [weak self] in animations: { [weak self] in
if let s: Switch = self { guard let s = self else {
s.button.x = .on == state ? s.onPosition : s.offPosition return
} }
s.button.x = .on == state ? s.onPosition : s.offPosition
}) { [weak self] _ in }) { [weak self] _ in
if let s: Switch = self { guard let s = self else {
return
}
s.isUserInteractionEnabled = true s.isUserInteractionEnabled = true
completion?(s) completion?(s)
} }
} }
} }
}
} }
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