简体   繁体   English

程序定义的约束Swift iOS

[英]Programmatically Defined Constraints Swift iOS

I have a UICollectionView . 我有一个UICollectionView I also have the following view which I generate in code: 我也有以下在代码中生成的视图:

let messageInputContainerView: UIView = {
    let view = UIView()
    view.backgroundColor = UIColor.whiteColor()
    return view
}()

Now, I want to add the messageInputContainerView so that it is attached to the bottom of the screen, and the UICollectionView is directly above it. 现在,我想添加messageInputContainerView以便将其附加到屏幕底部,而UICollectionView则直接位于其上方。 At the moment I have the following constraints : 目前,我有以下限制:

view.addSubview(messageInputContainerView)
    view.addConstraintsWithFormat("H:|[v0]|", views: messageInputContainerView)
    view.addConstraintsWithFormat("V:[v0(48)]", views: messageInputContainerView)

    bottomConstraint = NSLayoutConstraint(item: messageInputContainerView, attribute: .Bottom, relatedBy: .Equal, toItem: view, attribute: .Bottom, multiplier: 1, constant: 0)
    view.addConstraint(bottomConstraint!)

The problem is that the collectionView is now touching the bottom of the screen as well, and the two views overlap. 问题在于collectionView现在也正在触摸屏幕的底部,并且两个视图重叠。 I tried solving this with the following constraint : 我尝试解决以下约束:

let newConstraint = NSLayoutConstraint(item: messageInputContainerView, attribute: .Top, relatedBy: .Equal, toItem: self.collectionView! , attribute: .Bottom, multiplier: 1, constant: 0)

view.addConstraint(newConstraint)

however this just through a bunch of errors : 但是,这只是通过一系列错误:

 016-05-30 19:50:26.153 City[1858:79868] 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) ( "<NSAutoresizingMaskLayoutConstraint:0x7fece04d10e0 h=-&- v=-&- UICollectionView:0x7fece08c0a00.midY == UICollectionViewControllerWrapperView:0x7fece04125f0.midY>", "<NSAutoresizingMaskLayoutConstraint:0x7fece04d1150 h=-&- v=-&- UICollectionView:0x7fece08c0a00.height == UICollectionViewControllerWrapperView:0x7fece04125f0.height>", "<NSLayoutConstraint:0x7fece049a460 V:[UIView:0x7fece04992d0(48)]>", "<NSLayoutConstraint:0x7fece04d7620 UIView:0x7fece04992d0.bottom == UICollectionViewControllerWrapperView:0x7fece04125f0.bottom>", "<NSLayoutConstraint:0x7fece04a9990 V:[UICollectionView:0x7fece08c0a00]-(0)-[UIView:0x7fece04992d0]>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7fece04a9990 V:[UICollectionView:0x7fece08c0a00]-(0)-[UIView:0x7fece04992d0]> Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful. 

Please checkout the basic tutorials for autolayout. 请查看有关自动布局的基本教程 It will simply give You the needed background for IB and constraints. 它只会为您提供IB和约束所需的背景。

The debug log commonly all you need - it tells You the exact info for your problem. 调试日志通常只需要您所需要的-它会告诉您问题的确切信息。 Let's think about it in detail 让我们详细考虑一下

NSLayoutConstraint:0x7fece04a9990 V:[UICollectionView:0x7fece08c0a00]-(0)-[UIView:0x7fece04992d0]

UICollectionView:0x7fece08c0a00 - it's your collection view, it's simple UIView:0x7fece04992d0 - it's your messageInputContainerView - that's clear from the following constraint: UICollectionView:0x7fece08c0a00这是您的集合视图,它是简单的UIView:0x7fece04992d0这是您的messageInputContainerView-从以下约束中可以清楚地看出:

"<NSLayoutConstraint:0x7fece049a460 V:[UIView:0x7fece04992d0(48)]>"

which generated by this line of code: 这行代码生成的代码:

view.addConstraintsWithFormat("V:[v0(48)]", views: messageInputContainerView)

So, if you can see - uicollection view has NSAutoresizingMaskLayoutConstraint - so, those one were generated by default. 因此,如果您可以看到-uicollection视图具有NSAutoresizingMaskLayoutConstraint-那么,这些默认情况下已生成。 You have two ways - set up your collection view constraints via IB - set leading, trainling, top (to whatever value you need) and bottom constraint with the value 48. 您有两种方法-通过IB设置集合视图约束-使用值48设置超前,训练,顶部(为您需要的任何值)和底部约束。

The second one - remove autogenerated constraint programmatically and add new one, as following: 第二个-以编程方式删除自动生成的约束并添加新的约束,如下所示:

[collectionView removeConstraints: [collectionView constraints]]; 
        // top constraint
    view.addConstraint(NSLayoutConstraint(item: self.collectionView, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1, constant: 0))
    // leading constraint
    view.addConstraint(NSLayoutConstraint(item: self.collectionView, attribute: .Leading, relatedBy: .Equal, toItem: view, attribute: .Leading, multiplier: 1, constant: 0))
    // trailing constaint
    view.addConstraint(NSLayoutConstraint(item: self.collectionView, attribute: .Trailing, relatedBy: .Equal, toItem: view, attribute: .Trailing, multiplier: 1, constant: 0))
    // your constraint
    let newConstraint = NSLayoutConstraint(item: messageInputContainerView, attribute: .Top, relatedBy: .Equal, toItem: self.collectionView! , attribute: .Bottom, multiplier: 1, constant: 0)

    view.addConstraint(newConstraint)

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM