ios - Swift 3 以编程方式创建 UILabel 并添加 NSLayoutConstraints

标签 ios uilabel swift3 nslayoutconstraint programmatically-created

你好我正在尝试以编程方式创建一个标签并添加 NSLayoutConstraints 以便它在 super View 中居中,而不管屏幕大小和方向等。我已经看过但找不到可遵循的示例。这是我所拥有的:

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

关于ios - Swift 3 以编程方式创建 UILabel 并添加 NSLayoutConstraints,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40299190/

相关文章:

ios - Swift - 后台获取和延迟

ios - 使用多个方案和推送通知配置配置文件

iphone - 在 iOS 的核心数据中存储复杂数据结构的最佳实践是什么?

iPhone:在 UIScrollview 中处理 400 个 UILabels 的最佳方法

ios - 为什么 NSLineBreakByWordWrapping 有时不起作用

终端中的 Swift 3 编译器

ios - 域=NSURLError域代码=-1021 "request body stream exhausted"

ios - 适用于 iOS 平台的电子邮件应用程序示例

iphone - 根据 UILabel 大小展开 UITableViewCell

ios - 发布版本中的 Liker 错误 - 函数签名专门化 <Arg[2] = Exploded>