Last active
February 8, 2016 08:42
-
-
Save danstepanov/02d9a4eb6b801e94f1e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class MainViewController: UIViewController, UITextFieldDelegate { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.addSubview(textField) | |
view.setNeedsUpdateConstraints() | |
} | |
func textFieldShouldReturn(textField: UITextField) -> Bool { | |
// Dismisses the Keyboard by making the text field resign | |
// first responder | |
textField.resignFirstResponder() | |
// returns false. Instead of adding a line break, the text | |
// field resigns | |
return false | |
} | |
override func updateViewConstraints() { | |
textFieldConstraints() | |
super.updateViewConstraints() | |
} | |
func textFieldConstraints() { | |
NSLayoutConstraint( | |
item: textField, | |
attribute: .CenterX, | |
relatedBy: .Equal, | |
toItem: view, | |
attribute: .CenterX, | |
multiplier: 1.0, | |
constant: 0.0) | |
.active = true | |
NSLayoutConstraint( | |
item: textField, | |
attribute: .Width, | |
relatedBy: .Equal, | |
toItem: view, | |
attribute: .Width, | |
multiplier: 0.8, | |
constant: 0.0) | |
.active = true | |
NSLayoutConstraint( | |
item: textField, | |
attribute: .Top, | |
relatedBy: .Equal, | |
toItem: view, | |
attribute: .Bottom, | |
multiplier: 0.1, | |
constant: 0.0) | |
.active = true | |
} | |
lazy var textField: UITextField! = { | |
let view = UITextField() | |
view.translatesAutoresizingMaskIntoConstraints = false | |
view.borderStyle = .RoundedRect | |
return view | |
}() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment