以编程方式自动布局不起作用

Autolayout programmatically doesn#39;t work(以编程方式自动布局不起作用)

本文介绍了以编程方式自动布局不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式将 UIImageView 添加到视图并添加约束以使其垂直和水平居中.使用故事板,它可以工作,但不能以编程方式.

类 ViewController: UIViewController {覆盖 func viewDidLoad() {super.viewDidLoad()var transitionImageView = UIImageView()transitionImageView.frame.size = CGSize(宽度: 60, 高度: 68)transitionImageView.contentMode = UIViewContentMode.ScaleToFillvar transitionImage = UIImage(named: "SoProxyLogo60pt")transitionImageView.image = 过渡图像self.view.addSubview(transitionImageView)//位置让transitionImageViewConstraintCenterX = NSLayoutConstraint(项目:transitionImageView,属性:.CenterX,relatedBy:.Equal,toItem:self.view,属性:.CenterX,乘数:1,常数:0)transitionImageViewConstraintCenterX.identifier = "过渡图像视图约束中心 X"让transitionImageViewConstraintCenterY = NSLayoutConstraint(项目:transitionImageView,属性:.CenterY,relatedBy:.Equal,toItem:self.view,属性:.CenterY,乘数:1,常数:0)transitionImageViewConstraintCenterY.identifier = "过渡图像视图约束中心 Y"self.view.addConstraint(transitionImageViewConstraintCenterX)self.view.addConstraint(transitionImageViewConstraintCenterY)}}

我遇到了这些错误:

<块引用>

2014-10-27 14:45:18.420 transitionTest1[3438:70b] 无法同时满足约束.以下列表中的至少一个约束可能是您不想要的.试试这个:(1)查看每个约束并尝试找出您不期望的;(2) 找到添加了一个或多个不需要的约束的代码并修复它.(注意:如果您看到不理解的 NSAutoresizingMaskLayoutConstraints,请参阅 UIView 属性 translatesAutoresizingMaskIntoConstraints 的文档)

<NSLayoutConstraint:0x7fbb5b210790 '转换图像视图约束中心 X' UIImageView:0x7fbb5af682d0.centerX == UIView:0x7fbb5af674f0.centerX>,<NSAutoresizingMaskLayoutConstraint:0x7fbb5b25​​eee0 h=--&v=--&UIImageView:0x7fbb5af682d0.midX == + 30>,<NSAutoresizingMaskLayoutConstraint:0x7fbb5b25​​8ee0 h=-&- v=-&- UIView:0x7fbb5af674f0.width == UIViewControllerWrapperView:0x7fbb5b25​​7e20.width>,<NSAutoresizingMaskLayoutConstraint:0x7fbb5b260840 h=-&- v=-&- UIViewControllerWrapperView:0x7fbb5b25​​7e20.width == UINavigationTransitionView:0x7fbb5af5b020.width>,<NSAutoresizingMaskLayoutConstraint:0x7fbb5b2612b0 h=-&- v=-&- UINavigationTransitionView:0x7fbb5af5b020.width == UILayoutContainerView:0x7fbb5af2adf0.width>,<NSAutoresizingMaskLayoutConstraint:0x7fbb5b261d40 h=-&- v=-&- UILayoutContainerView:0x7fbb5af2adf0.width == UIWindow:0x7fbb5b25​​1ba0.width>,<NSAutoresizingMaskLayoutConstraint:0x7fbb5b25​​ff30 h=--- v=--- H:[UIWindow:0x7fbb5b25​​1ba0(320)]>

<块引用>

将尝试通过打破约束来恢复

中断 objc_exception_throw 以在调试器中捕获它.中列出的 UIView 上的 UIConstraintBasedLayoutDebugging 类别中的方法也可能会有所帮助.

2014-10-27 14:45:18.421 transitionTest1[3438:70b] 无法同时满足约束.以下列表中的至少一个约束可能是您不想要的.试试这个:(1)查看每个约束并尝试找出您不期望的;(2) 找到添加了一个或多个不需要的约束的代码并修复它.(注意:如果您看到不理解的 NSAutoresizingMaskLayoutConstraints,请参阅 UIView 属性 translatesAutoresizingMaskIntoConstraints 的文档)

<NSLayoutConstraint:0x7fbb5b25​​2fd0 '过渡图像视图约束中心 Y' UIImageView:0x7fbb5af682d0.centerY == UIView:0x7fbb5af674f0.centerY>,<NSAutoresizingMaskLayoutConstraint:0x7fbb5b25​​f140 h=--&v=--&UIImageView:0x7fbb5af682d0.midY == + 34>,<NSAutoresizingMaskLayoutConstraint:0x7fbb5b25​​8f80 h=-&- v=-&- UIView:0x7fbb5af674f0.height == UIViewControllerWrapperView:0x7fbb5b25​​7e20.height>,<NSAutoresizingMaskLayoutConstraint:0x7fbb5b260930 h=-&- v=-&- UIViewControllerWrapperView:0x7fbb5b25​​7e20.height == UINavigationTransitionView:0x7fbb5af5b020.height>,<NSAutoresizingMaskLayoutConstraint:0x7fbb5b2613b0 h=-&- v=-&- UINavigationTransitionView:0x7fbb5af5b020.height == UILayoutContainerView:0x7fbb5af2adf0.height>,<NSAutoresizingMaskLayoutConstraint:0x7fbb5b261e30 h=-&- v=-&- UILayoutContainerView:0x7fbb5af2adf0.height == UIWindow:0x7fbb5b25​​1ba0.height>,<NSAutoresizingMaskLayoutConstraint:0x7fbb5b262730 h=--- v=--- V:[UIWindow:0x7fbb5b25​​1ba0(480)]>

<块引用>

将尝试通过打破约束来恢复

中断 objc_exception_throw 以在调试器中捕获它.中列出的 UIView 上的 UIConstraintBasedLayoutDebugging 类别中的方法也可能会有所帮助.

(这里很难有一个好的布局;))

我不明白为什么这些约束不起作用,它们似乎与情节提要中的相同

解决方案

已更新至 SWIFT 3

我测试了你的代码,发现问题是你漏掉了这一行:

transitionImageView.translatesAutoresizingMaskIntoConstraints = false

此外,如果您希望图像尺寸为 60x68,则需要为宽度和高度添加一些约束,如下所示:

let constW = NSLayoutConstraint(item: transitionImageView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 60)let constV = NSLayoutConstraint(item: transitionImageView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 68)

并将它们添加到您的 UIImageView

transitionImageView.addConstraint(constW)transitionImageView.addConstraint(constV)

否则 UIImageView 将是 UIImage 大小,即使您指定 CGSize (因为 setTranslatesAutoresizingMaskIntoConstraints(false)禁用您分配的 CGSize).

所以你的 viewDidLoad() 方法可能是这样的:

覆盖 func viewDidLoad() {super.viewDidLoad()让transitionImageView = UIImageView()transitionImageView.contentMode = UIViewContentMode.scaleToFill//不要忘记这一行transitionImageView.translatesAutoresizingMaskIntoConstraints = falselet transitionImage = UIImage(named: "SoProxyLogo60pt.jpg")transitionImageView.image = 过渡图像self.view.addSubview(transitionImageView)//位置let transitionImageViewConstraintCenterX = NSLayoutConstraint(item: transitionImageView, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 0)transitionImageViewConstraintCenterX.identifier = "过渡图像视图约束中心 X"让transitionImageViewConstraintCenterY = NSLayoutConstraint(项目:transitionImageView,属性:.centerY,relatedBy:.equal,toItem:self.view,属性:.centerY,乘数:1,常数:0)transitionImageViewConstraintCenterY.identifier = "过渡图像视图约束中心 Y"let constW = NSLayoutConstraint(item: transitionImageView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 60)let constV = NSLayoutConstraint(item: transitionImageView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 68)self.view.addConstraint(transitionImageViewConstraintCenterX)self.view.addConstraint(transitionImageViewConstraintCenterY)transitionImageView.addConstraint(constW)//self.view.addConstraint(constW) 也可以transitionImageView.addConstraint(constV)//self.view.addConstraint(constV) 也可以}

I want to add programmatically an UIImageView to a view and add constraints to center it, vertically and horizontally. With the storyboard it's works but not programmatically.

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        var transitionImageView = UIImageView()
        transitionImageView.frame.size = CGSize(width: 60, height: 68)
        transitionImageView.contentMode = UIViewContentMode.ScaleToFill

        var transitionImage = UIImage(named: "SoProxyLogo60pt")
        transitionImageView.image = transitionImage
        self.view.addSubview(transitionImageView)

        // Position
        let transitionImageViewConstraintCenterX = NSLayoutConstraint(item: transitionImageView, attribute: .CenterX, relatedBy: .Equal, toItem: self.view, attribute: .CenterX, multiplier: 1, constant: 0)
        transitionImageViewConstraintCenterX.identifier = "Transition Image View Constraint Center X"
        let transitionImageViewConstraintCenterY = NSLayoutConstraint(item: transitionImageView, attribute: .CenterY, relatedBy: .Equal, toItem: self.view, attribute: .CenterY, multiplier: 1, constant: 0)
        transitionImageViewConstraintCenterY.identifier = "Transition Image View Constraint Center Y"

        self.view.addConstraint(transitionImageViewConstraintCenterX)
        self.view.addConstraint(transitionImageViewConstraintCenterY)        
    }
}

I've got these errors :

2014-10-27 14:45:18.420 transitionTest1[3438:70b] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)

<NSLayoutConstraint:0x7fbb5b210790 'Transition Image View Constraint Center X' UIImageView:0x7fbb5af682d0.centerX == UIView:0x7fbb5af674f0.centerX>,
<NSAutoresizingMaskLayoutConstraint:0x7fbb5b25eee0 h=--& v=--& UIImageView:0x7fbb5af682d0.midX == + 30>,
<NSAutoresizingMaskLayoutConstraint:0x7fbb5b258ee0 h=-&- v=-&- UIView:0x7fbb5af674f0.width == UIViewControllerWrapperView:0x7fbb5b257e20.width>,
<NSAutoresizingMaskLayoutConstraint:0x7fbb5b260840 h=-&- v=-&- UIViewControllerWrapperView:0x7fbb5b257e20.width == UINavigationTransitionView:0x7fbb5af5b020.width>,
<NSAutoresizingMaskLayoutConstraint:0x7fbb5b2612b0 h=-&- v=-&- UINavigationTransitionView:0x7fbb5af5b020.width == UILayoutContainerView:0x7fbb5af2adf0.width>,
<NSAutoresizingMaskLayoutConstraint:0x7fbb5b261d40 h=-&- v=-&- UILayoutContainerView:0x7fbb5af2adf0.width == UIWindow:0x7fbb5b251ba0.width>,
<NSAutoresizingMaskLayoutConstraint:0x7fbb5b25ff30 h=--- v=--- H:[UIWindow:0x7fbb5b251ba0(320)]>

Will attempt to recover by breaking constraint

Break on objc_exception_throw to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.

2014-10-27 14:45:18.421 transitionTest1[3438:70b] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)

<NSLayoutConstraint:0x7fbb5b252fd0 'Transition Image View Constraint Center Y' UIImageView:0x7fbb5af682d0.centerY == UIView:0x7fbb5af674f0.centerY>,
<NSAutoresizingMaskLayoutConstraint:0x7fbb5b25f140 h=--& v=--& UIImageView:0x7fbb5af682d0.midY == + 34>,
<NSAutoresizingMaskLayoutConstraint:0x7fbb5b258f80 h=-&- v=-&- UIView:0x7fbb5af674f0.height == UIViewControllerWrapperView:0x7fbb5b257e20.height>,
<NSAutoresizingMaskLayoutConstraint:0x7fbb5b260930 h=-&- v=-&- UIViewControllerWrapperView:0x7fbb5b257e20.height == UINavigationTransitionView:0x7fbb5af5b020.height>,
<NSAutoresizingMaskLayoutConstraint:0x7fbb5b2613b0 h=-&- v=-&- UINavigationTransitionView:0x7fbb5af5b020.height == UILayoutContainerView:0x7fbb5af2adf0.height>,
<NSAutoresizingMaskLayoutConstraint:0x7fbb5b261e30 h=-&- v=-&- UILayoutContainerView:0x7fbb5af2adf0.height == UIWindow:0x7fbb5b251ba0.height>,
<NSAutoresizingMaskLayoutConstraint:0x7fbb5b262730 h=--- v=--- V:[UIWindow:0x7fbb5b251ba0(480)]>

Will attempt to recover by breaking constraint

Break on objc_exception_throw to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.

(It's hard to have a good layout here ;) )

I don't understand why these constraints doesn't work, they seem to be the same that those in the storyboard

解决方案

UPDATED TO SWIFT 3

I tested your code and the problem i found was that you missed this line:

transitionImageView.translatesAutoresizingMaskIntoConstraints = false

Also if you want your image size is 60x68 you need to add a couple of constraints for the width and height like this:

let constW = NSLayoutConstraint(item: transitionImageView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 60)

let constV = NSLayoutConstraint(item: transitionImageView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 68)

and add them to your UIImageView

transitionImageView.addConstraint(constW)
transitionImageView.addConstraint(constV)

Otherwise the UIImageView will be the UIImage size even if you assign CGSize (because setTranslatesAutoresizingMaskIntoConstraints(false) disables the CGSize you assigned).

So your viewDidLoad() method could be like this:

override func viewDidLoad() {
    super.viewDidLoad()
    let transitionImageView = UIImageView()
    transitionImageView.contentMode = UIViewContentMode.scaleToFill

    //Don't forget this line
    transitionImageView.translatesAutoresizingMaskIntoConstraints = false

    let transitionImage = UIImage(named: "SoProxyLogo60pt.jpg")
    transitionImageView.image = transitionImage
    self.view.addSubview(transitionImageView)

    // Position
    let transitionImageViewConstraintCenterX = NSLayoutConstraint(item: transitionImageView, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 0)
    transitionImageViewConstraintCenterX.identifier = "Transition Image View Constraint Center X"
    let transitionImageViewConstraintCenterY = NSLayoutConstraint(item: transitionImageView, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1, constant: 0)
    transitionImageViewConstraintCenterY.identifier = "Transition Image View Constraint Center Y"

    let constW = NSLayoutConstraint(item: transitionImageView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 60)

    let constV = NSLayoutConstraint(item: transitionImageView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 68)

    self.view.addConstraint(transitionImageViewConstraintCenterX)
    self.view.addConstraint(transitionImageViewConstraintCenterY)
    transitionImageView.addConstraint(constW) //self.view.addConstraint(constW) also works
    transitionImageView.addConstraint(constV) //self.view.addConstraint(constV) also works
}

这篇关于以编程方式自动布局不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:以编程方式自动布局不起作用

基础教程推荐