Ios 为什么以编程方式使用自动布局约束进行居中不起作用?

Ios 为什么以编程方式使用自动布局约束进行居中不起作用?,ios,swift,autolayout,ios-autolayout,Ios,Swift,Autolayout,Ios Autolayout,我正试图以编程方式将我的GridView与Autolayout约束置于中心位置。已建议并建议使用的答案 NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.gridView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, c

我正试图以编程方式将我的
GridView
Autolayout
约束置于中心位置。已建议并建议使用的答案

NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal,
            toItem: self.gridView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant:0)
我添加了如下约束:

func setupConstraints() {

    let centerX = NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal,
        toItem: self.gridView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant:0)

    let centerY = NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal,
        toItem: self.gridView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant:0)

    self.view.addConstraints([centerX, centerY])
}

然而,这似乎不适合我。运行应用程序时,视图位于左上角,控制台显示有关问题的任意输出,同时满足约束条件


我认为您没有添加此代码

self.gridView.translatesAutoresizingMaskIntoConstraints = false

我想你没有添加这个代码

self.gridView.translatesAutoresizingMaskIntoConstraints = false
这应该起作用:

func setupConstraints() {

    self.viewToCenter.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint(item: self.viewToCenter, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal,
        toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant:0).active = true

    NSLayoutConstraint(item: self.viewToCenter, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal,
        toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant:0).active = true
}
您的第一项和第二项错误,请不要使用
addConstraints
,因为它已经或将被弃用(iOS这应该可以:

func setupConstraints() {

    self.viewToCenter.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint(item: self.viewToCenter, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal,
        toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant:0).active = true

    NSLayoutConstraint(item: self.viewToCenter, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal,
        toItem: self.view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant:0).active = true
}

您的第一个和第二个项目是错误的,不要使用
addConstraints
,因为它已经或将被弃用(iOS)您的视图中还有哪些其他约束处于活动状态?目前为止没有任何约束…我想稍后使用视觉格式语言
[gridView]为大小添加一个约束(100@100)]
,但它现在不在里面。“控制台显示了关于同时满足约束的问题的疯狂输出”,这就是问题所在,而不是“疯狂”“它的信息量非常大。你需要阅读该输出。如果不能,请在你的问题中引用它。停止挥舞,开始思考。在你的视图中还有哪些约束是有效的?到目前为止还没有……我想稍后使用视觉格式语言
[gridView]为大小添加一个约束。”(100@100)]
,但它现在不在里面。“控制台显示了关于同时满足约束的问题的疯狂输出”,这就是问题所在,而不是“疯狂”";它的信息量非常大。你需要阅读该输出。如果你不能,那么在你的问题中引用它。停止挥舞,开始思考。这是可行的,我实际上认为问题在于我没有设置
self.viewToCenter.translatesAutoReshizingMackintoConstraints=false
…但是,居中仍然无法使用此代码。要使例如,更简单的是,我将我的
GridView
替换为一个简单的
UIView
,但它仍然不起作用(实际上,视图根本不显示,当我在Xcode中使用视图调试时它甚至不在那里…)。我想我应该在另一个问题中讨论这个问题。你有没有尝试过设置宽度/高度约束?自动布局可能会不时打破正常的帧!我使用了
NSLayoutConstraint.constraintsWithVisualFormat(“[gridView])(100@100)],选项:NSLayoutFormatOptions.AlignAllCenterX,指标:零,视图:[“gridView”:self.gridView])
,…奇怪的是,当我在
setupConstraints()
之前和之后记录帧时,两次输出都是
,只是视图消失了。顺便说一句。这是旧的自动布局语法。尝试使用“self.vieToCenter.centerXAnchor.constraintEqualsAnchor(self.view.centerXAnchor)”.active=true“解决问题的方法就像我在这里告诉你的那样。自动布局可能会破坏视图的框架,因此有时最好创建大小限制。如果你有时间进行实验并学习一些,你可能想尝试一下我的小语法包装器,它可以在Swift中使用新的自动布局语法:我认为这很有效问题是,我没有将
self.viewToCenter.translatesAutoresizingMaskIntoConstraints=false
…但是,此代码仍然无法居中。为了使示例更简单,我将
GridView
替换为简单的
UIView
,但它仍然不起作用(事实上,视图根本不显示,当我在Xcode中使用视图调试时它甚至不存在…。不过我想我应该在另一个问题中讨论这个问题。您是否尝试过设置宽度/高度约束?自动布局可能会不时打破正常帧!我使用了
NSLayoutConstraint.constraintsWithVisualFormat(“[gridView])(100@100)],options:NSLayoutFormatOptions.AlignAllCenterX,metrics:nil,views:[“gridView”:self.gridView])
,…奇怪的是,当我在
setupConstraints()之前和之后记录帧时
,两次输出均为
,但视图已消失。顺便说一句,这是旧的自动布局语法。请尝试使用“self.vieToCenter.centerXAnchor.constraintEqualsAnchor(self.view.centerXAnchor)”.active=true“问题的解决方式就像我在这里告诉你的那样。AutoLayout可能会破坏视图的框架,因此有时最好创建大小限制。如果你有时间进行实验并学习一些,你可能想尝试一下我的小语法包装器,用于Swift中的新AutoLayout语法: