Skip to content

Instantly share code, notes, and snippets.

@danstepanov
Last active February 8, 2016 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danstepanov/02d9a4eb6b801e94f1e7 to your computer and use it in GitHub Desktop.
Save danstepanov/02d9a4eb6b801e94f1e7 to your computer and use it in GitHub Desktop.
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