add state for MessageToolbar; show sendButton when have text input

This commit is contained in:
nixzhu
2015-03-25 10:54:40 +08:00
parent 1a163e99a8
commit b65ea34945
2 changed files with 67 additions and 12 deletions
@@ -77,7 +77,7 @@ class ConversationViewController: UIViewController {
updateUIWithKeyboardChange = true
messageToolbar.messageTextField.delegate = self
//messageToolbar.messageTextField.delegate = self
}
// MARK: Private
@@ -202,14 +202,4 @@ extension ConversationViewController: UICollectionViewDataSource, UICollectionVi
}
}
// MARK: UITextFieldDelegate
extension ConversationViewController: UITextFieldDelegate {
func textFieldShouldReturn(textField: UITextField) -> Bool {
return true
}
}
+66 -1
View File
@@ -8,9 +8,28 @@
import UIKit
enum MessageToolbarState {
case Default
case TextInput
}
@IBDesignable
class MessageToolbar: UIToolbar {
var state: MessageToolbarState = .Default {
willSet {
switch newValue {
case .Default:
micButton.hidden = false
sendButton.hidden = true
case .TextInput:
micButton.hidden = true
sendButton.hidden = false
}
}
}
lazy var cameraButton: UIButton = {
let button = UIButton()
button.setImage(UIImage(named: "item_camera"), forState: .Normal)
@@ -22,6 +41,8 @@ class MessageToolbar: UIToolbar {
let textField = UITextField()
textField.borderStyle = .RoundedRect
textField.returnKeyType = .Send
textField.delegate = self
textField.addTarget(self, action: "editingChangedInTextField:", forControlEvents: UIControlEvents.EditingChanged)
return textField
}()
@@ -32,11 +53,20 @@ class MessageToolbar: UIToolbar {
return button
}()
lazy var sendButton: UIButton = {
let button = UIButton()
button.setTitle(NSLocalizedString("Send", comment: ""), forState: .Normal)
button.setTitleColor(UIColor.yepTintColor(), forState: .Normal)
return button
}()
override func didMoveToSuperview() {
super.didMoveToSuperview()
makeUI()
state = .Default
}
func makeUI() {
@@ -50,10 +80,14 @@ class MessageToolbar: UIToolbar {
self.addSubview(micButton)
micButton.setTranslatesAutoresizingMaskIntoConstraints(false)
self.addSubview(sendButton)
sendButton.setTranslatesAutoresizingMaskIntoConstraints(false)
let viewsDictionary = [
"cameraButton": cameraButton,
"messageTextField": messageTextField,
"micButton": micButton
"micButton": micButton,
"sendButton": sendButton,
]
let constraintsV = NSLayoutConstraint.constraintsWithVisualFormat("V:|[cameraButton(==micButton)]|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)
@@ -62,6 +96,37 @@ class MessageToolbar: UIToolbar {
NSLayoutConstraint.activateConstraints(constraintsV)
NSLayoutConstraint.activateConstraints(constraintsH)
let sendButtonConstraintCenterY = NSLayoutConstraint(item: sendButton, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: cameraButton, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
let sendButtonConstraintHeight = NSLayoutConstraint(item: sendButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: cameraButton, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0)
let sendButtonConstraintsH = NSLayoutConstraint.constraintsWithVisualFormat("H:[messageTextField][sendButton(==cameraButton)]|", options: NSLayoutFormatOptions.AlignAllCenterY, metrics: nil, views: viewsDictionary)
NSLayoutConstraint.activateConstraints([sendButtonConstraintCenterY])
NSLayoutConstraint.activateConstraints([sendButtonConstraintHeight])
NSLayoutConstraint.activateConstraints(sendButtonConstraintsH)
}
// Mark: TextField
func editingChangedInTextField(textfiled: UITextField) {
if textfiled.text.isEmpty {
self.state = .Default
} else {
self.state = .TextInput
}
}
}
// MARK: UITextFieldDelegate
extension MessageToolbar: UITextFieldDelegate {
func textFieldShouldReturn(textField: UITextField) -> Bool {
return true
}
}