Skip to content

Commit de446b6

Browse files
committed
Make the button color of the keyboard dismiss accessory view IBInspectable
1 parent 0f6e703 commit de446b6

3 files changed

Lines changed: 62 additions & 17 deletions

File tree

Example/Example/Main.storyboard

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
<dependencies>
44
<deployment identifier="iOS"/>
55
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9530"/>
6+
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
67
</dependencies>
78
<scenes>
89
<!--ICInputAccessory-->
910
<scene sceneID="ssv-qC-OfH">
1011
<objects>
1112
<tableViewController id="0fq-Xw-Z0G" customClass="StoryboardViewController" customModule="Example" customModuleProvider="target" sceneMemberID="viewController">
12-
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="static" style="grouped" separatorStyle="default" rowHeight="44" sectionHeaderHeight="18" sectionFooterHeight="18" id="ZTV-zA-HhW">
13+
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="static" style="grouped" separatorStyle="default" allowsSelection="NO" rowHeight="44" sectionHeaderHeight="18" sectionFooterHeight="18" id="ZTV-zA-HhW">
1314
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
1415
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
1516
<color key="backgroundColor" red="0.93725490199999995" green="0.93725490199999995" blue="0.95686274510000002" alpha="1" colorSpace="calibratedRGB"/>
@@ -31,6 +32,21 @@
3132
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="hiq-lt-L5I" id="dNb-Qm-7pq">
3233
<rect key="frame" x="0.0" y="0.0" width="600" height="43.5"/>
3334
<autoresizingMask key="autoresizingMask"/>
35+
<subviews>
36+
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="ICKeyboardDismissTextField" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="qdb-Ab-nYe" customClass="ICKeyboardDismissTextField" customModule="ICInputAccessory">
37+
<rect key="frame" x="18" y="12" width="564" height="20"/>
38+
<fontDescription key="fontDescription" type="system" pointSize="16"/>
39+
<textInputTraits key="textInputTraits"/>
40+
<connections>
41+
<outlet property="keyboardAccessoryView" destination="zaj-ho-SsM" id="wlb-FZ-bH0"/>
42+
</connections>
43+
</textField>
44+
</subviews>
45+
<constraints>
46+
<constraint firstAttribute="trailingMargin" secondItem="qdb-Ab-nYe" secondAttribute="trailing" constant="10" id="8T6-yk-3Bl"/>
47+
<constraint firstItem="qdb-Ab-nYe" firstAttribute="centerY" secondItem="dNb-Qm-7pq" secondAttribute="centerY" id="UB2-Jc-XXc"/>
48+
<constraint firstItem="qdb-Ab-nYe" firstAttribute="leading" secondItem="dNb-Qm-7pq" secondAttribute="leadingMargin" constant="10" id="WQl-qX-FOh"/>
49+
</constraints>
3450
</tableViewCellContentView>
3551
</tableViewCell>
3652
</cells>
@@ -56,6 +72,16 @@
5672
<navigationItem key="navigationItem" title="ICInputAccessory" id="Q3g-An-b4N"/>
5773
</tableViewController>
5874
<placeholder placeholderIdentifier="IBFirstResponder" id="4Lj-fL-J07" userLabel="First Responder" sceneMemberID="firstResponder"/>
75+
<view contentMode="scaleToFill" id="zaj-ho-SsM" customClass="ICKeyboardDismissAccessoryView" customModule="ICInputAccessory">
76+
<rect key="frame" x="0.0" y="0.0" width="320" height="60"/>
77+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
78+
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
79+
<userDefinedRuntimeAttributes>
80+
<userDefinedRuntimeAttribute type="color" keyPath="buttonColor">
81+
<color key="value" red="0.0" green="0.50196081400000003" blue="1" alpha="1" colorSpace="calibratedRGB"/>
82+
</userDefinedRuntimeAttribute>
83+
</userDefinedRuntimeAttributes>
84+
</view>
5985
</objects>
6086
<point key="canvasLocation" x="1520" y="377"/>
6187
</scene>

Source/ICKeyboardDismissAccessoryView.swift

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,16 @@
2626

2727
import UIKit
2828

29+
@IBDesignable
2930
public class ICKeyboardDismissAccessoryView: UIView {
3031

32+
/// The background color of the button to dismiss keyboard.
33+
@IBInspectable var buttonColor: UIColor = Constants.ButtonColor {
34+
didSet {
35+
dismissButton.backgroundColor = buttonColor
36+
}
37+
}
38+
3139
public private(set) lazy var dismissButton: UIButton = {
3240
let _button = UIButton()
3341
let resources = NSBundle(forClass: self.dynamicType)
@@ -41,6 +49,12 @@ public class ICKeyboardDismissAccessoryView: UIView {
4149
return _button
4250
}()
4351

52+
private struct Constants {
53+
static let ButtonColor = UIColor(red:0.21, green:0.2, blue:0.19, alpha:0.5)
54+
static let EdgePadding = CGFloat(7)
55+
static let InteractiveSize = CGSize(width: 44, height: 44)
56+
}
57+
4458
// MARK: - Initialization
4559

4660
public override init(frame: CGRect) {
@@ -66,14 +80,16 @@ public class ICKeyboardDismissAccessoryView: UIView {
6680
return false
6781
}
6882

69-
// MARK: - Private Methods
83+
// MARK: - NSKeyValueCoding
7084

71-
private struct Constants {
72-
static let ButtonColor = UIColor(red:0.21, green:0.2, blue:0.19, alpha:0.5)
73-
static let EdgePadding = CGFloat(7)
74-
static let InteractiveSize = CGSize(width: 44, height: 44)
85+
public override func setValue(value: AnyObject?, forUndefinedKey key: String) {
86+
if let color = value as? UIColor where key == "buttonColor" {
87+
buttonColor = color
88+
}
7589
}
7690

91+
// MARK: - Private Methods
92+
7793
private func setUpSubviews() {
7894
backgroundColor = UIColor.clearColor()
7995

@@ -101,7 +117,7 @@ public class ICKeyboardDismissAccessoryView: UIView {
101117
))
102118
}
103119

104-
// MARK: - Public Methods
120+
// MARK: - Internal Methods
105121

106122
class func requiredHeight() -> CGFloat {
107123
return Constants.InteractiveSize.height + Constants.EdgePadding * 2

Source/ICKeyboardDismissTextField.swift

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,16 @@
2626

2727
import UIKit
2828

29+
@IBDesignable
2930
public class ICKeyboardDismissTextField: UITextField {
3031

31-
private lazy var accessoryView: UIView = {
32-
let _accessory = ICKeyboardDismissAccessoryView()
33-
_accessory.dismissButton.addTarget(self, action: Selector("dismiss:"), forControlEvents: .TouchUpInside)
34-
return _accessory
35-
}()
32+
@IBOutlet public var keyboardAccessoryView: ICKeyboardDismissAccessoryView! {
33+
didSet {
34+
if UI_USER_INTERFACE_IDIOM() != .Phone { return }
35+
keyboardAccessoryView.dismissButton.addTarget(self, action: Selector("dismiss:"), forControlEvents: .TouchUpInside)
36+
inputAccessoryView = keyboardAccessoryView
37+
}
38+
}
3639

3740
// MARK: - Initialization
3841

@@ -41,7 +44,7 @@ public class ICKeyboardDismissTextField: UITextField {
4144
setUpAccessoryView()
4245
}
4346

44-
required public init?(coder aDecoder: NSCoder) {
47+
public required init?(coder aDecoder: NSCoder) {
4548
super.init(coder: aDecoder)
4649
setUpAccessoryView()
4750
}
@@ -50,23 +53,23 @@ public class ICKeyboardDismissTextField: UITextField {
5053

5154
public override func becomeFirstResponder() -> Bool {
5255
if UI_USER_INTERFACE_IDIOM() == .Phone {
53-
accessoryView.alpha = 1
56+
keyboardAccessoryView.alpha = 1
5457
}
5558
return super.becomeFirstResponder()
5659
}
5760

5861
// MARK: - Private Methods
5962

6063
private func setUpAccessoryView() {
61-
if UI_USER_INTERFACE_IDIOM() == .Phone {
62-
inputAccessoryView = accessoryView
64+
if keyboardAccessoryView == nil {
65+
keyboardAccessoryView = ICKeyboardDismissAccessoryView()
6366
}
6467
}
6568

6669
@IBAction private func dismiss(sender: UIButton) {
6770
resignFirstResponder()
6871
UIView.animateWithDuration(0.3) {
69-
self.accessoryView.alpha = 0
72+
self.keyboardAccessoryView.alpha = 0
7073
}
7174
}
7275

0 commit comments

Comments
 (0)