Swift 以编程方式向视图添加约束
下面是我添加约束的简单代码:Swift 以编程方式向视图添加约束,swift,constraints,Swift,Constraints,下面是我添加约束的简单代码: import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let contentView = UIView() contentView.backgroundColor = UIColor.greenColor() contentView.fr
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let contentView = UIView()
contentView.backgroundColor = UIColor.greenColor()
contentView.frame.size.width = 250
contentView.frame.size.height = 100
contentView.center = view.center
view.addSubview(contentView)
let userNameLabel = UILabel()
userNameLabel.translatesAutoresizingMaskIntoConstraints = false
userNameLabel.text = "Username:"
contentView.addSubview(userNameLabel)
let passwordLabel = UILabel()
passwordLabel.translatesAutoresizingMaskIntoConstraints = false
passwordLabel.text = "Password:"
contentView.addSubview(passwordLabel)
let usernameTextField = UITextField()
usernameTextField.translatesAutoresizingMaskIntoConstraints = false
usernameTextField.borderStyle = .RoundedRect
contentView.addSubview(usernameTextField)
let passwordTextField = UITextField()
passwordTextField.translatesAutoresizingMaskIntoConstraints = false
passwordTextField.borderStyle = .RoundedRect
contentView.addSubview(passwordTextField)
let submitButton = UIButton()
submitButton.translatesAutoresizingMaskIntoConstraints = false
submitButton.setTitle("Submit", forState: .Normal)
submitButton.setTitleColor(UIColor.redColor(), forState: .Normal)
submitButton.setTitleColor(UIColor.redColor(), forState: .Highlighted)
contentView.addSubview(submitButton)
// For Username
NSLayoutConstraint(item: userNameLabel, attribute: .Leading, relatedBy: .Equal, toItem: contentView, attribute: .LeadingMargin, multiplier: 1, constant: 0).active = true
NSLayoutConstraint(item: userNameLabel, attribute: .Top, relatedBy: .Equal, toItem: contentView, attribute: .Top, multiplier: 1, constant: 10).active = true
NSLayoutConstraint(item: usernameTextField, attribute: .Trailing, relatedBy: .Equal, toItem: contentView, attribute: .TrailingMargin, multiplier: 1.0, constant: 0.0).active = true
NSLayoutConstraint(item: usernameTextField, attribute: .Leading, relatedBy: .Equal, toItem: userNameLabel, attribute: .Trailing, multiplier: 1.0, constant: 3).active = true
NSLayoutConstraint(item: userNameLabel, attribute: .Baseline, relatedBy: .Equal, toItem: usernameTextField, attribute: .Baseline, multiplier: 1.0, constant: 0.0).active = true
// For Password
NSLayoutConstraint(item: passwordLabel, attribute: .Leading, relatedBy: .Equal, toItem: contentView, attribute: .LeadingMargin, multiplier: 1, constant: 0).active = true
NSLayoutConstraint(item: passwordLabel, attribute: .Top, relatedBy: .Equal, toItem: userNameLabel, attribute: .Bottom, multiplier: 1, constant: 15).active = true
NSLayoutConstraint(item: passwordTextField, attribute: .Trailing, relatedBy: .Equal, toItem: contentView, attribute: .TrailingMargin, multiplier: 1.0, constant: 0.0).active = true
NSLayoutConstraint(item: passwordTextField, attribute: .Leading, relatedBy: .Equal, toItem: passwordLabel, attribute: .Trailing, multiplier: 1.0, constant: 3).active = true
NSLayoutConstraint(item: passwordLabel, attribute: .Baseline, relatedBy: .Equal, toItem: passwordTextField, attribute: .Baseline, multiplier: 1.0, constant: 0.0).active = true
// For Button
NSLayoutConstraint(item: submitButton, attribute: .Top, relatedBy: .Equal, toItem: passwordLabel, attribute: .Bottom, multiplier: 1.0, constant: 8.0).active = true
NSLayoutConstraint(item: submitButton, attribute: .CenterX, relatedBy: .Equal, toItem: contentView, attribute: .CenterX, multiplier: 1.0, constant: 0.0).active = true
}
}
但是为什么看起来像这样,我希望passWordTextField
的前导是3
到passWordLabel
的尾随:
我看了这一行,没有发现任何问题:
NSLayoutConstraint(item: passwordTextField, attribute: .Leading, relatedBy: .Equal, toItem: passwordLabel, attribute: .Trailing, multiplier: 1.0, constant: 3).active = true
任何帮助都将不胜感激。谢谢问题在于标签和文本字段具有相同的拥抱优先级。您需要定义优先顺序。例如:
userNameLabel.setContentHuggingPriority(251, forAxis: .Horizontal)
passwordLabel.setContentHuggingPriority(251, forAxis: .Horizontal)
问题是label和textfield具有相同的拥抱优先级。您需要定义优先顺序。例如:
userNameLabel.setContentHuggingPriority(251, forAxis: .Horizontal)
passwordLabel.setContentHuggingPriority(251, forAxis: .Horizontal)
问题在于:这一行:
NSLayoutConstraint(item: passwordTextField, attribute: .Leading, relatedBy: .Equal, toItem: passwordLabel, attribute: .Trailing, multiplier: 1.0, constant: 3).active = true
应在此行之前显示:
NSLayoutConstraint(item: passwordTextField, attribute: .Trailing, relatedBy: .Equal, toItem: contentView, attribute: .TrailingMargin, multiplier: 1.0, constant: 0.0).active = true
密码的代码如下所示:
// For Password
NSLayoutConstraint(item: passwordLabel, attribute: .Leading, relatedBy: .Equal, toItem: contentView, attribute: .LeadingMargin, multiplier: 1, constant: 0).active = true
NSLayoutConstraint(item: passwordLabel, attribute: .Top, relatedBy: .Equal, toItem: userNameLabel, attribute: .Bottom, multiplier: 1, constant: 15).active = true
NSLayoutConstraint(item: passwordTextField, attribute: .Leading, relatedBy: .Equal, toItem: passwordLabel, attribute: .Trailing, multiplier: 1.0, constant: 3).active = true
NSLayoutConstraint(item: passwordTextField, attribute: .Trailing, relatedBy: .Equal, toItem: contentView, attribute: .TrailingMargin, multiplier: 1.0, constant: 0.0).active = true
NSLayoutConstraint(item: passwordLabel, attribute: .Baseline, relatedBy: .Equal, toItem: passwordTextField, attribute: .Baseline, multiplier: 1.0, constant: 0.0).active = true
问题在于:这一行:
NSLayoutConstraint(item: passwordTextField, attribute: .Leading, relatedBy: .Equal, toItem: passwordLabel, attribute: .Trailing, multiplier: 1.0, constant: 3).active = true
应在此行之前显示:
NSLayoutConstraint(item: passwordTextField, attribute: .Trailing, relatedBy: .Equal, toItem: contentView, attribute: .TrailingMargin, multiplier: 1.0, constant: 0.0).active = true
密码的代码如下所示:
// For Password
NSLayoutConstraint(item: passwordLabel, attribute: .Leading, relatedBy: .Equal, toItem: contentView, attribute: .LeadingMargin, multiplier: 1, constant: 0).active = true
NSLayoutConstraint(item: passwordLabel, attribute: .Top, relatedBy: .Equal, toItem: userNameLabel, attribute: .Bottom, multiplier: 1, constant: 15).active = true
NSLayoutConstraint(item: passwordTextField, attribute: .Leading, relatedBy: .Equal, toItem: passwordLabel, attribute: .Trailing, multiplier: 1.0, constant: 3).active = true
NSLayoutConstraint(item: passwordTextField, attribute: .Trailing, relatedBy: .Equal, toItem: contentView, attribute: .TrailingMargin, multiplier: 1.0, constant: 0.0).active = true
NSLayoutConstraint(item: passwordLabel, attribute: .Baseline, relatedBy: .Equal, toItem: passwordTextField, attribute: .Baseline, multiplier: 1.0, constant: 0.0).active = true
成功了。因为我认为UILabel会根据其中的文本自动设置宽度。但是为什么userNameLabel工作而passwordLabel不工作。但是当我更改passwordLabel上的文本时,它看起来很糟糕。不,问题不是这样。请再检查一下。好的,我知道是什么问题。让我更新一下我的答案。它是有效的,但它是随机决定哪一个优先,因为你可以从你的截图上看出。现在,标签已按优先级排列,其行为将如预期的那样。您仍然可以在标签之间添加一个等宽约束,以使文本视图在相同的x坐标处开始。这很有效。因为我认为UILabel会根据其中的文本自动设置宽度。但是为什么userNameLabel工作而passwordLabel不工作。但是当我更改passwordLabel上的文本时,它看起来很糟糕。不,问题不是这样。请再检查一下。好的,我知道是什么问题。让我更新一下我的答案。它是有效的,但它是随机决定哪一个优先,因为你可以从你的截图上看出。现在,标签已按优先级排列,其行为将如预期的那样。您仍然可以在标签之间添加等宽约束,以使文本视图从相同的x坐标开始。