Tag:

Swift 3以编程方式创buildUILabel并添加NSLayoutConstraints

你好,我正在试图创build一个标签编程,并添加NSLayoutConstraints,使它在超级视图中居中,无论屏幕的大小和方向等我看了,但没有find一个示例遵循。 这是我有什么: let codedLabel:UILabel = UILabel() codedLabel.frame = CGRect(x: 100, y: 100, width: 200, height: 200) codedLabel.textAlignment = .center codedLabel.text = alertText codedLabel.numberOfLines=1 codedLabel.textColor=UIColor.red codedLabel.font=UIFont.systemFont(ofSize: 22) codedLabel.backgroundColor=UIColor.lightGray let heightConstraint:NSLayoutConstraint = NSLayoutConstraint(item: codedLabel, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 200) let widthConstraint:NSLayoutConstraint = NSLayoutConstraint(item: codedLabel, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, […]