簡體   English   中英

Swift 3以編程方式創建UILabel並添加NSLayoutConstraints

[英]Swift 3 Create UILabel programmatically and add NSLayoutConstraints

您好我正在嘗試以編程方式創建標簽並添加NSLayoutConstraints,以便它在superview中居中,無論屏幕大小和方向等等。我看了但是找不到一個示例。 這是我有的:

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, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 200)

codedLabel.addConstraints([heightConstraint, widthConstraint])

let verticalConstraint:NSLayoutConstraint = NSLayoutConstraint(item: codedLabel, attribute: NSLayoutAttribute.centerY, relatedBy: NSLayoutRelation.equal, toItem: self.contentView, attribute: NSLayoutAttribute.centerY, multiplier: 1, constant: 0)

let horizontalConstraint:NSLayoutConstraint = NSLayoutConstraint(item: codedLabel, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self.contentView, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0)

self.contentView.addConstraints([verticalConstraint, horizontalConstraint])

self.contentView.addSubview(codedLabel)

NSLayoutAnchor是iOS 9中的新功能,它極大地清除了約束語法。

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

self.contentView.addSubview(codedLabel)
codedLabel.translatesAutoresizingMaskIntoConstraints = false
codedLabel.heightAnchor.constraint(equalToConstant: 200).isActive = true
codedLabel.widthAnchor.constraint(equalToConstant: 200).isActive = true
codedLabel.centerXAnchor.constraint(equalTo: codedLabel.superview!.centerXAnchor).isActive = true
codedLabel.centerYAnchor.constraint(equalTo: codedLabel.superview!.centerYAnchor).isActive = true

請參閱下面的代碼作為示例

let lblNew = UILabel()
lblNew.backgroundColor = UIColor.blue
lblNew.text = "Test"
lblNew.textColor = UIColor.white
lblNew.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(lblNew)

let widthConstraint = NSLayoutConstraint(item: lblNew, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 300)
let heightConstraint = NSLayoutConstraint(item: lblNew, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 200)
var constraints = NSLayoutConstraint.constraints(
    withVisualFormat: "V:[superview]-(<=1)-[label]",
    options: NSLayoutFormatOptions.alignAllCenterX,
    metrics: nil,
    views: ["superview":view, "label":lblNew])

view.addConstraints(constraints)

// Center vertically
constraints = NSLayoutConstraint.constraints(
    withVisualFormat: "H:[superview]-(<=1)-[label]",
    options: NSLayoutFormatOptions.alignAllCenterY,
    metrics: nil,
    views: ["superview":view, "label":lblNew])

view.addConstraints(constraints)

view.addConstraints([ widthConstraint, heightConstraint])
let myLabel: UILabel = {
  let lb = UILabel()
  lb.translatesAutoresizingMaskIntoConstraints = false
  lb.textAlignment = .center
  lb.numberOfLines = 1
  lb.textColor = UIColor.black
  lb.font=UIFont.systemFont(ofSize: 22)
  lb.backgroundColor = UIColor.grey
  return lb
}()

super.viewDidLoad()方法中,您需要為視圖添加標簽並為標簽添加約束:

override func viewDidLoad() {
  super.viewDidLoad()
  self.view.addSubView(myLabel)
  setUpMyLabel()
}

func setUpMyLabel() {
  NSLayoutConstraint.activate([
            myLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            myLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            myLabel.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.7),
            myLabel.heightAnchor.constraint(equalToConstant: 50)])
}

我希望我的代碼可以幫到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM