Ios Swift UIView约束恒定高度更改时间获取布局约束警告

Ios Swift UIView约束恒定高度更改时间获取布局约束警告,ios,swift,Ios,Swift,在我的场景中,我在情节提要中实现了UIView,高度恒定为90。在这个UIView中,我有两个UIView高度25和另一个UICollectionView 54。现在,我试图通过编程将基本UIView高度从90降低到35,但我得到了LayoutConstraints警告。为正确的方法提供一些解决方案 下面是我正在使用的代码 BottomGalleryHeight.constant = 90 // Changing to 35 photoGalleryView.layoutIfNeeded()

在我的场景中,我在
情节提要
中实现了UIView,高度恒定为90。在这个UIView中,我有两个
UIView高度25
和另一个
UICollectionView 54
。现在,我试图通过编程将基本UIView高度从90降低到35,但我得到了
LayoutConstraints
警告。为正确的方法提供一些解决方案

下面是我正在使用的代码

BottomGalleryHeight.constant = 90 // Changing to 35
photoGalleryView.layoutIfNeeded()
我的警告错误

[LayoutConstraints] 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. 
(
    "<NSLayoutConstraint:0x282679360 UIView:0x102f30880.height == 25   (active)>",
    "<NSLayoutConstraint:0x282679450 UIView:0x102f306a0.height == 35   (active)>",
    "<NSLayoutConstraint:0x2826794a0 V:|-(0)-[UIView:0x102f30880]   (active, names: '|':UIView:0x102f306a0 )>",
    "<NSLayoutConstraint:0x28267e210 V:[UIView:0x102f30880]-(10)-[UICollectionView:0x10385e000]   (active)>",
    "<NSLayoutConstraint:0x28267d450 V:[UICollectionView:0x10385e000]-(1)-|   (active, names: '|':UIView:0x102f306a0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x282679360 UIView:0x102f30880.height == 25   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
[LayoutConstraints]无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。
试试这个:
(1) 看看每一个约束,试着找出你不期望的;
(2) 找到添加了不需要的约束的代码,然后修复它。
(
"",
"",
"",
"",
""
)
将尝试通过打破约束进行恢复
在UIViewAlertForUnsatifiableConstraints处创建一个符号断点,以便在调试器中捕获该断点。
中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。

下面是调整约束恒定高度的工作代码

// to update height constant
photoGalleryView.updateConstraint(attribute: NSLayoutAttribute.height, constant: 20.0)

extension UIView {    

    func updateConstraint(attribute: NSLayoutAttribute, constant: CGFloat) -> Void {
        if let constraint = (self.constraints.filter{$0.firstAttribute == attribute}.first) {
            constraint.constant = constant
            self.layoutIfNeeded()
        }
    }
}

下面是调整约束恒定高度的工作代码

// to update height constant
photoGalleryView.updateConstraint(attribute: NSLayoutAttribute.height, constant: 20.0)

extension UIView {    

    func updateConstraint(attribute: NSLayoutAttribute, constant: CGFloat) -> Void {
        if let constraint = (self.constraints.filter{$0.firstAttribute == attribute}.first) {
            constraint.constant = constant
            self.layoutIfNeeded()
        }
    }
}

下面是如何让它工作的

class VC: UIViewController {
    @IBOutlet weak var outerViewHeight: NSLayoutConstraint!
    @IBOutlet weak var innerViewHeight: NSLayoutConstraint!
    @IBOutlet weak var collectionViewHeight: NSLayoutConstraint!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func onTapButton(_ sender: UIButton) {
        outerViewHeight.constant = 35.0
        innerViewHeight.constant = 10.0
        collectionViewHeight.constant = 25.0

        UIView.animate(withDuration: 0.3) {
            self.view.layoutIfNeeded()
        }
    }
}

以下是如何使其正常工作的方法

class VC: UIViewController {
    @IBOutlet weak var outerViewHeight: NSLayoutConstraint!
    @IBOutlet weak var innerViewHeight: NSLayoutConstraint!
    @IBOutlet weak var collectionViewHeight: NSLayoutConstraint!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func onTapButton(_ sender: UIButton) {
        outerViewHeight.constant = 35.0
        innerViewHeight.constant = 10.0
        collectionViewHeight.constant = 25.0

        UIView.animate(withDuration: 0.3) {
            self.view.layoutIfNeeded()
        }
    }
}

你能给我们展示一下它的视觉效果吗?@Rakeshashstri更新了我的问题。有白色基础UIView 90高度和内部基础UIView顶部蓝色UIView 25高度和底部集合视图54高度。我在故事板布局中给出的高度是恒定的。但是代码基础我正在更改基础UIView(白色)最初为35和90。您需要更改内部UIView和collectionView的高度约束,以便在外部视图(即35)的高度范围内进行调整。请显示您为视图创建的所有约束。(你可以添加IB的截图)@AndrewRomanov我已经更新了我的问题。请检查一下。你能给我们展示一下它的视觉效果吗?@Rakeshashstri更新了我的问题。有白色基础UIView 90高度和内部基础UIView顶部蓝色UIView 25高度和底部集合视图54高度。我在故事板布局中给出的高度是恒定的。但是代码基础我正在更改基础UIView(白色)最初为35和90。您需要更改内部UIView和collectionView的高度约束,以便在外部视图(即35)的高度范围内进行调整。请显示您为视图创建的所有约束。(你可以添加IB的截图)@AndrewRomanov我已经更新了我的问题。请检查一下。非常感谢你这么大的帮助。顺便说一句,下面贴出的答案也不错。我需要哪一个来做正确的标记?:)那完全是你的选择。你认为更好的解决方案应该被接受。非常感谢你这么大的帮助。顺便说一句,下面贴出的答案也不错。我需要哪一个来做正确的标记?:)那完全是你的选择。你认为更好的解决方案应该被接受。