ios 设置属性的center_ios – 以编程方式添加CenterX/CenterY约束

iOS 8及更高版本的更新:

[谢谢@ChrisHatton提醒我更新答案]

从iOS 8开始,您可以通过将其活动属性设置为true来激活约束。这使得约束能够将自己添加到正确的视图中。通过将包含约束的数组传递给NSLayoutConstraint.activateConstraints()可以一次激活多个约束,

let label = UILabel(frame: CGRectZero)

label.text = "Nothing to show"

label.textAlignment = .Center

label.backgroundColor = .redColor() // Set background color to see if label is centered

label.translatesAutoresizingMaskIntoConstraints = false

self.tableView.addSubview(label)

let widthConstraint = NSLayoutConstraint(item: label, attribute: .Width, relatedBy: .Equal,

toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 250)

let heightConstraint = NSLayoutConstraint(item: label, attribute: .Height, relatedBy: .Equal,

toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100)

let xConstraint = NSLayoutConstraint(item: label, attribute: .CenterX, relatedBy: .Equal, toItem: self.tableView, attribute: .CenterX, multiplier: 1, constant: 0)

let yConstraint = NSLayoutConstraint(item: label, attribute: .CenterY, relatedBy: .Equal, toItem: self.tableView, attribute: .CenterY, multiplier: 1, constant: 0)

NSLayoutConstraint.activateConstraints([widthConstraint, heightConstraint, xConstraint, yConstraint])

原始答案:

约束引用了self.tableView。由于您将标签添加为self.tableView的子视图,因此需要将约束添加到“common ancestor”中:

self.tableView.addConstraint(xConstraint)

self.tableView.addConstraint(yConstraint)

由于@mustafa和@kcstricks在注释中指出,您需要将label.translatesAutoresizingMaskIntoConstraints设置为false。执行此操作时,还需要使用约束来指定标签的宽度和高度,因为不再使用该框架。最后,您还应该将textAlignment设置为.Center,以使您的文本居中在标签中。

var label = UILabel(frame: CGRectZero)

label.text = "Nothing to show"

label.textAlignment = .Center

label.backgroundColor = UIColor.redColor() // Set background color to see if label is centered

label.translatesAutoresizingMaskIntoConstraints = false

self.tableView.addSubview(label)

let widthConstraint = NSLayoutConstraint(item: label, attribute: .Width, relatedBy: .Equal,

toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 250)

label.addConstraint(widthConstraint)

let heightConstraint = NSLayoutConstraint(item: label, attribute: .Height, relatedBy: .Equal,

toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100)

label.addConstraint(heightConstraint)

let xConstraint = NSLayoutConstraint(item: label, attribute: .CenterX, relatedBy: .Equal, toItem: self.tableView, attribute: .CenterX, multiplier: 1, constant: 0)

let yConstraint = NSLayoutConstraint(item: label, attribute: .CenterY, relatedBy: .Equal, toItem: self.tableView, attribute: .CenterY, multiplier: 1, constant: 0)

self.tableView.addConstraint(xConstraint)

self.tableView.addConstraint(yConstraint)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值