Commit 74fe8155 by Daniel Dahan

working SearchBarView

parent 787a893d
{
"images" : [
{
"idiom" : "universal",
"filename" : "ic_close_white.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "ic_close_white_2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "ic_close_white_3x.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
\ No newline at end of file
......@@ -2,8 +2,6 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
......@@ -33,6 +31,9 @@
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
......@@ -41,5 +42,7 @@
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
</dict>
</plist>
......@@ -36,11 +36,8 @@ import UIKit
import Material
class ViewController: UIViewController, MaterialSwitchDelegate {
/// Reference for MaterialSwitch.
private var materialSwitch: MaterialSwitch!
/// Reference for NavigationBarView.
private var searchBarView: SearchBarView = SearchBarView()
private var searchBarView: SearchBarView = SearchBarView(frame: CGRectNull)
override func viewDidLoad() {
super.viewDidLoad()
......@@ -64,36 +61,49 @@ class ViewController: UIViewController, MaterialSwitchDelegate {
private func prepareSearchBarView() {
searchBarView.statusBarStyle = .LightContent
searchBarView.backgroundColor = MaterialColor.blue.base
searchBarView.textField.attributedPlaceholder = NSAttributedString(string:"Search", attributes: [NSForegroundColorAttributeName: MaterialColor.white])
searchBarView.textField.clearButtonMode = .WhileEditing
searchBarView.textField.tintColor = MaterialColor.white
view.addSubview(searchBarView)
}
/// Prepares the Small MaterialSwitch.
private func prepareSmallMaterialSwitch() {
materialSwitch = MaterialSwitch(state: .Off, style: .Light, size: .Small)
materialSwitch.delegate = self
let image: UIImage? = UIImage(named: "ic_alarm_white")
var image = UIImage(named: "ic_close_white")
let clearButton: FlatButton = FlatButton()
clearButton.pulseColor = MaterialColor.white
clearButton.setImage(image, forState: .Normal)
clearButton.setImage(image, forState: .Highlighted)
searchBarView.clearButton = clearButton
image = UIImage(named: "ic_alarm_white")
let alarmButton: FlatButton = FlatButton()
alarmButton.pulseColor = MaterialColor.white
alarmButton.setImage(image, forState: .Normal)
alarmButton.setImage(image, forState: .Highlighted)
let button: FlatButton = FlatButton()
button.setTitle("Back Button", forState: .Normal)
searchBarView.placeholder = "Search"
searchBarView.tintColor = MaterialColor.white
searchBarView.textColor = MaterialColor.white
searchBarView.placeholderTextColor = MaterialColor.white
searchBarView.leftControls = [materialSwitch]
searchBarView.rightControls = [alarmButton]
view.addSubview(searchBarView)
searchBarView.translatesAutoresizingMaskIntoConstraints = false
MaterialLayout.height(view, child: searchBarView, height: 70)
MaterialLayout.alignFromTop(view, child: searchBarView)
MaterialLayout.alignToParentHorizontally(view, child: searchBarView)
}
/// Prepares the Small MaterialSwitch.
private func prepareSmallMaterialSwitch() {
let materialSwitch: MaterialSwitch = MaterialSwitch(state: .Off, style: .Light, size: .Small)
materialSwitch.delegate = self
searchBarView.leftControls = [materialSwitch]
}
/// Prepares the Normal MaterialSwitch.
private func prepareNormalMaterialSwitch() {
materialSwitch = MaterialSwitch(state: .On, style: .Light, size: .Normal)
let materialSwitch: MaterialSwitch = MaterialSwitch(state: .On, style: .Light, size: .Normal)
materialSwitch.center = view.center
materialSwitch.y -= 100
materialSwitch.delegate = self
materialSwitch.backgroundColor = MaterialColor.green.base
view.addSubview(materialSwitch)
materialSwitch.translatesAutoresizingMaskIntoConstraints = false
MaterialLayout.size(view, child: materialSwitch, width: materialSwitch.width + 100, height: materialSwitch.height + 100)
......@@ -103,7 +113,7 @@ class ViewController: UIViewController, MaterialSwitchDelegate {
/// Prepares the Large MaterialSwitch.
private func prepareLargeMaterialSwitch() {
let image: UIImage? = UIImage(named: "ic_alarm_white_18pt")
materialSwitch = MaterialSwitch(state: .Off, style: .Light, size: .Large)
let materialSwitch: MaterialSwitch = MaterialSwitch(state: .Off, style: .Light, size: .Large)
materialSwitch.center = view.center
materialSwitch.y -= 50
materialSwitch.delegate = self
......@@ -115,7 +125,7 @@ class ViewController: UIViewController, MaterialSwitchDelegate {
/// Prepares the Light On enabled = false MaterialSwitch.
private func prepareLightOnDisabledMaterialSwitch() {
materialSwitch = MaterialSwitch(state: .On, style: .Light, size: .Normal)
let materialSwitch: MaterialSwitch = MaterialSwitch(state: .On, style: .Light, size: .Normal)
materialSwitch.enabled = false
materialSwitch.center = view.center
materialSwitch.delegate = self
......@@ -125,7 +135,7 @@ class ViewController: UIViewController, MaterialSwitchDelegate {
/// Prepares the Light Off enabled = false MaterialSwitch.
private func prepareLightOffDisabledMaterialSwitch() {
materialSwitch = MaterialSwitch(state: .Off, style: .Light, size: .Normal)
let materialSwitch: MaterialSwitch = MaterialSwitch(state: .Off, style: .Light, size: .Normal)
materialSwitch.enabled = false
materialSwitch.center = view.center
materialSwitch.y += 50
......@@ -136,7 +146,7 @@ class ViewController: UIViewController, MaterialSwitchDelegate {
/// Prepares the Dark On enabled = false MaterialSwitch.
private func prepareDarkOnDisabledMaterialSwitch() {
materialSwitch = MaterialSwitch(state: .On, style: .Dark, size: .Normal)
let materialSwitch: MaterialSwitch = MaterialSwitch(state: .On, style: .Dark, size: .Normal)
materialSwitch.enabled = false
materialSwitch.center = view.center
materialSwitch.y += 100
......@@ -147,7 +157,7 @@ class ViewController: UIViewController, MaterialSwitchDelegate {
/// Prepares the Dark On enabled = false MaterialSwitch.
private func prepareDarkOffDisabledMaterialSwitch() {
materialSwitch = MaterialSwitch(state: .Off, style: .Dark, size: .Normal)
let materialSwitch: MaterialSwitch = MaterialSwitch(state: .Off, style: .Dark, size: .Normal)
materialSwitch.enabled = false
materialSwitch.center = view.center
materialSwitch.y += 150
......
......@@ -263,6 +263,17 @@ public class MaterialSwitch: UIControl {
styleForState(switchState)
}
public override func intrinsicContentSize() -> CGSize {
switch switchSize {
case .Small:
return CGSizeMake(30, 25)
case .Normal:
return CGSizeMake(40, 30)
case .Large:
return CGSizeMake(50, 40)
}
}
/**
Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepareView method
......@@ -468,5 +479,9 @@ public class MaterialSwitch: UIControl {
button.frame = CGRectMake(px, (height - buttonDiameter) / 2, buttonDiameter, buttonDiameter)
onPosition = width - px - buttonDiameter
offPosition = px
if .On == switchState {
button.x = onPosition
}
}
}
......@@ -31,9 +31,10 @@
import UIKit
public class SearchBarView : MaterialView {
/**
:name: statusBarStyle
*/
/// The UITextField for the searchBar.
public private(set) lazy var textField: TextField = TextField()
/// StatusBar style.
public var statusBarStyle: UIStatusBarStyle = UIApplication.sharedApplication().statusBarStyle {
didSet {
UIApplication.sharedApplication().statusBarStyle = statusBarStyle
......@@ -54,9 +55,6 @@ public class SearchBarView : MaterialView {
}
}
/// SearchBar textField.
public private(set) lazy var textField: TextField = TextField()
/**
:name: leftControls
*/
......@@ -75,6 +73,50 @@ public class SearchBarView : MaterialView {
}
}
/// The UIImage for the clear icon.
public var clearButton: UIButton? {
didSet {
if let v: UIButton = clearButton {
v.contentEdgeInsets = UIEdgeInsetsZero
v.addTarget(self, action: "handleClearButton", forControlEvents: .TouchUpInside)
}
textField.rightView = clearButton
}
}
/// TintColor for searchBar.
public override var tintColor: UIColor? {
didSet {
textField.tintColor = tintColor
}
}
/// TextColor for searchBar.
public var textColor: UIColor? {
didSet {
textField.textColor = textColor
}
}
/// Placeholder textColor.
public var placeholderTextColor: UIColor? {
didSet {
if let v: String = textField.placeholder {
textField.attributedPlaceholder = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: MaterialColor.white])
}
}
}
/// A wrapper for searchBar.placeholder.
public var placeholder: String? {
didSet {
textField.placeholder = placeholder
if let v: String = textField.placeholder {
textField.attributedPlaceholder = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: MaterialColor.white])
}
}
}
/**
:name: init
*/
......@@ -104,6 +146,11 @@ public class SearchBarView : MaterialView {
prepareProperties(leftControls, rightControls: rightControls)
}
public override func layoutSubviews() {
super.layoutSubviews()
reloadView()
}
public override func didMoveToSuperview() {
super.didMoveToSuperview()
reloadView()
......@@ -113,68 +160,61 @@ public class SearchBarView : MaterialView {
:name: reloadView
*/
public func reloadView() {
layoutIfNeeded()
// clear constraints so new ones do not conflict
removeConstraints(constraints)
for v in subviews {
v.removeFromSuperview()
if v != textField {
v.removeFromSuperview()
}
}
// Size of single grid column.
let g: CGFloat = width / CGFloat(0 < grid.columns ? grid.columns : 1)
grid.views = []
textField.grid.columns = grid.columns
// leftControls
if let v: Array<UIControl> = leftControls {
if 0 < v.count {
for c in v {
let w: CGFloat = c.intrinsicContentSize().width
if let b: UIButton = c as? UIButton {
b.contentEdgeInsets = UIEdgeInsetsZero
}
// Size of single grid column.
let g: CGFloat = width / CGFloat(grid.columns)
c.grid.columns = 0 == g ? 1 : Int(ceil(w / g))
textField.grid.columns -= c.grid.columns
for c in v {
var w: CGFloat = c.frame.width
if 0 == w {
if let b: UIButton = c as? UIButton {
b.contentEdgeInsets = UIEdgeInsetsZero
}
w = c.intrinsicContentSize().width
}
c.grid.columns = Int(ceil(w / g))
textField.grid.columns -= c.grid.columns
addSubview(c)
grid.views?.append(c)
}
addSubview(c)
grid.views?.append(c)
}
}
addSubview(textField)
grid.views?.append(textField)
// rightControls
if let v: Array<UIControl> = rightControls {
if 0 < v.count {
for c in v {
let w: CGFloat = c.intrinsicContentSize().width
if let b: UIButton = c as? UIButton {
b.contentEdgeInsets = UIEdgeInsetsZero
}
// Size of single grid column.
let g: CGFloat = width / CGFloat(grid.columns)
c.grid.columns = 0 == g ? 1 : Int(ceil(w / g))
textField.grid.columns -= c.grid.columns
for c in v {
var w: CGFloat = c.frame.width
if 0 == w {
if let b: UIButton = c as? UIButton {
b.contentEdgeInsets = UIEdgeInsetsZero
}
w = c.intrinsicContentSize().width
}
c.grid.columns = Int(ceil(w / g))
textField.grid.columns -= c.grid.columns
addSubview(c)
grid.views?.append(c)
}
addSubview(c)
grid.views?.append(c)
}
}
/// Prepare the clearButton
if let v: UIButton = clearButton {
v.frame = CGRectMake(0, 0, textField.height, textField.height)
}
grid.reloadLayout()
}
......@@ -193,6 +233,11 @@ public class SearchBarView : MaterialView {
prepareTextField()
}
/// Clears the textField text.
internal func handleClearButton() {
textField.text = ""
}
/**
:name: prepareProperties
*/
......@@ -201,7 +246,12 @@ public class SearchBarView : MaterialView {
self.rightControls = rightControls
}
/// Prepares the textField.
private func prepareTextField() {
textField.placeholder = "Search"
textField.backgroundColor = MaterialColor.clear
textField.clearButtonMode = .Never
textField.rightViewMode = .WhileEditing
addSubview(textField)
}
}
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