Guest User

Untitled

a guest
Dec 30th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.03 KB | None | 0 0
  1. (
  2. "<NSLayoutConstraint:0x7ac57370 H:[UIView:0x7a96b6f0(0)]>",
  3. "<NSLayoutConstraint:0x7ac57400 H:|-(8)-[UILabel:0x7a96bb50'Username:'] (Names: '|':UIView:0x7a96b6f0 )>",
  4. "<NSLayoutConstraint:0x7ac57430 UILabel:0x7a96bb50'Username:'.trailing == UITextField:0x7a961020.leading + 8>",
  5. "<NSLayoutConstraint:0x7ac57520 UITextField:0x7a961020.trailing == UIView:0x7a96b6f0.trailing - 8>" )
  6.  
  7. import UIKit
  8.  
  9. class ViewController: UIViewController {
  10.  
  11. let centerView = UIView()
  12. let usernameLabel = UILabel()
  13. let passwordLabel = UILabel()
  14. let usernameField = UITextField()
  15. let passwordField = UITextField()
  16. let submitButton = UIButton()
  17.  
  18. override func viewDidLoad() {
  19. super.viewDidLoad()
  20.  
  21. centerView.backgroundColor = UIColor.yellowColor()
  22. usernameLabel.text = "Username:"
  23. passwordLabel.text = "Password:"
  24. usernameField.borderStyle = .RoundedRect
  25. passwordField.borderStyle = .RoundedRect
  26. submitButton.setTitle("Submit!", forState: .Normal)
  27. submitButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
  28. submitButton.setTitleColor(UIColor.blueColor(), forState: .Highlighted)
  29.  
  30. view.addSubview(centerView)
  31. self.centerView.addSubview(usernameField)
  32. self.centerView.addSubview(passwordField)
  33. self.centerView.addSubview(usernameLabel)
  34. self.centerView.addSubview(submitButton)
  35.  
  36. let constraintCenterViewHeight = NSLayoutConstraint(item: centerView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 0)
  37. let constraintCenterViewWidth = NSLayoutConstraint(item: centerView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 0)
  38.  
  39. let constraintCenterViewCenterX = NSLayoutConstraint(item: centerView, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1.0, constant: 0)
  40. let constraintCenterViewCenterY = NSLayoutConstraint(item: centerView, attribute: .CenterY, relatedBy: .Equal, toItem: view, attribute: .CenterY, multiplier: 1.0, constant: 0)
  41.  
  42. let constraintUsernameLabelHLeading = NSLayoutConstraint(item: usernameLabel, attribute: .Leading, relatedBy: .Equal, toItem: centerView, attribute: .Leading, multiplier: 1.0, constant: 8)
  43. let constraintUsernameLabelHTrailing = NSLayoutConstraint(item: usernameLabel, attribute: .Trailing, relatedBy: .Equal, toItem: usernameField, attribute: .Leading, multiplier: 1.0, constant: 8)
  44. let constraintUsernameLabelAlignBottom = NSLayoutConstraint(item: usernameLabel, attribute: .Bottom, relatedBy: .Equal, toItem: usernameField, attribute: .Bottom, multiplier: 1.0, constant: 0)
  45.  
  46. let constraintUsernameFieldVTop = NSLayoutConstraint(item: usernameField, attribute: .Top, relatedBy: .Equal, toItem: centerView, attribute: .Top, multiplier: 1.0, constant: 8)
  47. let constraintUsernameFieldHTrailing = NSLayoutConstraint(item: usernameField, attribute: .Trailing, relatedBy: .Equal, toItem: centerView, attribute: .Trailing, multiplier: 1.0, constant: -8)
  48. let constraintUsernameFieldVBottom = NSLayoutConstraint(item: usernameField, attribute: .Bottom, relatedBy: .Equal, toItem: passwordField, attribute: .Top, multiplier: 1.0, constant: 8)
  49.  
  50. let constraintPasswordLabelHLeading = NSLayoutConstraint(item: passwordLabel, attribute: .Leading, relatedBy: .Equal, toItem: centerView, attribute: .Leading, multiplier: 1.0, constant: 8)
  51. let constraintPasswordLabelHTrailing = NSLayoutConstraint(item: passwordLabel, attribute: .Trailing, relatedBy: .Equal, toItem: passwordField, attribute: .Leading, multiplier: 1.0, constant: 8)
  52. let constraintPasswordLabelAlignBottom = NSLayoutConstraint(item: passwordLabel, attribute: .Bottom, relatedBy: .Equal, toItem: passwordField, attribute: .Bottom, multiplier: 1.0, constant: 0)
  53.  
  54. let constraintPasswordFieldHTrailing = NSLayoutConstraint(item: passwordField, attribute: .Trailing, relatedBy: .Equal, toItem: centerView, attribute: .Trailing, multiplier: 1.0, constant: -8)
  55.  
  56. centerView.setTranslatesAutoresizingMaskIntoConstraints(false)
  57. usernameLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
  58. usernameField.setTranslatesAutoresizingMaskIntoConstraints(false)
  59. passwordField.setTranslatesAutoresizingMaskIntoConstraints(false)
  60. passwordLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
  61. // submitButton.setTranslatesAutoresizingMaskIntoConstraints(false)
  62.  
  63. NSLayoutConstraint.activateConstraints([constraintCenterViewHeight, constraintCenterViewWidth, constraintCenterViewCenterX, constraintCenterViewCenterY, constraintUsernameLabelHLeading,
  64. constraintUsernameLabelHTrailing, constraintUsernameLabelAlignBottom, constraintUsernameFieldVTop, constraintUsernameFieldHTrailing, constraintUsernameFieldVBottom, constraintPasswordLabelHLeading, constraintPasswordLabelHTrailing, constraintPasswordLabelAlignBottom, constraintPasswordFieldHTrailing])
  65. }
  66.  
  67. override func didReceiveMemoryWarning() {
  68. super.didReceiveMemoryWarning()
  69. // Dispose of any resources that can be recreated.
  70. }
  71.  
  72.  
  73. }
  74.  
  75. self.centerView.addSubview(passwordLabel)
  76.  
  77. let constraintCenterViewHeight = NSLayoutConstraint(item: centerView, attribute: .Height,
  78. relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0,
  79. constant: 100)
  80.  
  81. let constraintCenterViewWidth = NSLayoutConstraint(item: centerView, attribute: .Width,
  82. relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0,
  83. constant: 300)
  84.  
  85. // changed to -8
  86. let constraintUsernameLabelHTrailing = NSLayoutConstraint(item: usernameLabel,
  87. attribute: .Trailing, relatedBy: .Equal, toItem: usernameField,
  88. attribute: .Leading, multiplier: 1.0, constant: -8)
  89.  
  90. // changed to -8
  91. let constraintUsernameFieldVBottom = NSLayoutConstraint(item: usernameField,
  92. attribute: .Bottom, relatedBy: .Equal, toItem: passwordField,
  93. attribute: .Top, multiplier: 1.0, constant: -8)
  94.  
  95. // changed to -8
  96. let constraintPasswordLabelHTrailing = NSLayoutConstraint(item: passwordLabel,
  97. attribute: .Trailing, relatedBy: .Equal, toItem: passwordField,
  98. attribute: .Leading, multiplier: 1.0, constant: -8)
  99.  
  100. class ViewController: UIViewController {
  101.  
  102. var label = UILabel()
  103. var button = UIButton()
  104. var stackView = UIStackView()
  105.  
  106. override func viewDidLoad() {
  107. super.viewDidLoad()
  108. view.backgroundColor = UIColor.cyan
  109. navigationItem.title = "VCTitle"
  110. setupUI()
  111. }
  112.  
  113. private func setupUI(){
  114.  
  115. label.backgroundColor = UIColor.blue
  116. label.heightAnchor.constraint(equalToConstant: 50).isActive = true
  117. label.widthAnchor.constraint(equalToConstant: 80).isActive = true
  118.  
  119. button.backgroundColor = UIColor.purple
  120. button.heightAnchor.constraint(equalToConstant: 30).isActive = true
  121. button.widthAnchor.constraint(equalToConstant: 10).isActive = true
  122.  
  123. setupStackView()
  124.  
  125. stackView.addArrangedSubview(label)
  126. stackView.addArrangedSubview(button)
  127. stackView.translatesAutoresizingMaskIntoConstraints = false
  128. view.addSubview(stackView) // AA
  129.  
  130.  
  131. }
  132.  
  133. private func setupStackView(){
  134.  
  135. stackView.axis = UILayoutConstraintAxis.vertical
  136. stackView.distribution = UIStackViewDistribution.equalSpacing
  137. stackView.alignment = UIStackViewAlignment.center
  138. stackView.spacing = 15
  139.  
  140. stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true // B1
  141. stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true // B2
  142.  
  143. }
  144.  
  145.  
  146. }
  147.  
  148. class ViewController: UIViewController {
  149.  
  150. var label = UILabel()
  151. var button = UIButton()
  152. var stackView = UIStackView()
  153.  
  154. override func viewDidLoad() {
  155. super.viewDidLoad()
  156. view.backgroundColor = UIColor.cyan
  157. navigationItem.title = "VCTitle"
  158. setupUI()
  159. }
  160.  
  161. private func setupUI(){
  162.  
  163. label.backgroundColor = UIColor.blue
  164. label.heightAnchor.constraint(equalToConstant: 50).isActive = true
  165. label.widthAnchor.constraint(equalToConstant: 80).isActive = true
  166.  
  167. button.backgroundColor = UIColor.purple
  168. button.heightAnchor.constraint(equalToConstant: 30).isActive = true
  169. button.widthAnchor.constraint(equalToConstant: 10).isActive = true
  170.  
  171. setupStackView()
  172.  
  173. stackView.addArrangedSubview(label)
  174. stackView.addArrangedSubview(button)
  175. stackView.translatesAutoresizingMaskIntoConstraints = false
  176. view.addSubview(stackView) //AA
  177.  
  178. // The 2 lines below are now in the right place.
  179. stackView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true // B1
  180. stackView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true // B2
  181.  
  182.  
  183. }
  184.  
  185. private func setupStackView(){
  186.  
  187. stackView.axis = UILayoutConstraintAxis.vertical
  188. stackView.distribution = UIStackViewDistribution.equalSpacing
  189. stackView.alignment = UIStackViewAlignment.center
  190. stackView.spacing = 15
  191.  
  192. }
  193. }
Add Comment
Please, Sign In to add comment