以编程方式添加的centerX / CenterY约束 [英] Programmatically Add CenterX/CenterY Constraints

查看:290
本文介绍了以编程方式添加的centerX / CenterY约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如果没有什么证明不显示任何部分的的UITableViewController。我添加了一个标签说明用户没有什么要显示与此code:

I have a UITableViewController that doesn't display any sections if there is nothing to show. I've added a label to indicate to the user that there is nothing to display with this code:

label = UILabel(frame: CGRectMake(20, 20, 250, 100))
label.text = "Nothing to show"
self.tableView.addSubview(label)

但现在,我希望它是水平,垂直居中。通常情况下,我会选择在截图中强调了两个选择(加药粥高度和宽度):

But now, I want it to be centered Horizontally and Vertically. Normally, I would choose the two options highlighted (plus ones for height and width) in the screenshot:

我试过以下code与错误添加约束,但是应用程序崩溃:

I've tried the following code to add the constraints but the app crashes with error:

label = UILabel(frame: CGRectMake(20, 20, 250, 100))
label.text = "Nothing to show"

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)

label.addConstraint(xConstraint)
label.addConstraint(yConstraint)

错误:

When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
2014-12-23 08:17:36.755 [982:227877] *** Assertion failure in -[UILabel _layoutEngine_didAddLayoutConstraint:roundingAdjustment:mutuallyExclusiveConstraints:], /SourceCache/UIKit/UIKit-3318.16.21/NSLayoutConstraint_UIKitAdditions.m:560

由于应用程序支持的设备的旋转标签应该总是在水平和垂直居中。

The label should always center horizontally and vertically because the app supports rotation of the device.

我是什么做错了吗?我如何成功添加这些约束条件?

What am I doing wrong? How do I add these constraints successfully?

谢谢!

推荐答案

更新适用于iOS 8及更高版本:

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

[Thanks @ChrisHatton for reminding me to update the answer]

由于iOS的8,您可以并且应该通过他们的有效属性设置为真正激活你的约束。这使得约束自己添加到适当的意见。你可以通过包含约束数组为 NSLayoutConstraint.activateConstraints()

As of iOS 8, you can and should activate your constraints by setting their active property to true. This enables the constraints to add themselves to the proper views. You can activate multiple constraints at once by passing an array containing the constraints to 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 的子视图,约束需要被添加到共同祖先:

The constraints make reference to self.tableView. Since you are adding the label as a subview of self.tableView, the constraints need to be added to the "common ancestor":

   self.tableView.addConstraint(xConstraint)
   self.tableView.addConstraint(yConstraint)


由于@mustafa和@kcstricks在评论中指出的那样,你需要设置 label.translatesAutoresizingMaskIntoConstraints 。当你这样做,你还需要有约束指定宽度高度标签,因为框架不再用来。最后,您还应该设置 textAlignment .Center ,使您的文本在您的标签居中。


As @mustafa and @kcstricks pointed out in the comments, you need to set label.translatesAutoresizingMaskIntoConstraints to false. When you do this, you also need to specify the width and height of the label with constraints because the frame no longer is used. Finally, you also should set the textAlignment to .Center so that your text is centered in your label.

    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)

这篇关于以编程方式添加的centerX / CenterY约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆