简体   繁体   English

以编程方式添加约束不尊重优先级

[英]Adding constraints programmatically is not respecting priority

The below is in a loop which adds views to a superview. 以下是将视图添加到超级视图的循环。 Each subview is the remove button and information to the left.....they are sitting in a parent view... 每个子视图都是“删除”按钮和左侧的信息.....它们位于父视图中...

Each subview is constrained to the view above...and the top view is constrained to the top edge of the parent view..all added programatically. 每个子视图都限于上面的视图...,顶视图只限于父视图的顶部边缘。所有这些均以编程方式添加。

The remove button will delete one at a time 删除按钮将一次删除一个

我不添加次要优先级800最高锚点的精细版本

Once I add a secondary top constraint to each view to account for the deleting then I get the following.... 当我向每个视图添加次要的顶部约束以说明删除时,我将得到以下信息:...

在此处输入图片说明

var horizontalConstraint1 = NSLayoutConstraint()
var horizontalConstraint2 = NSLayoutConstraint()
var verticalConstraint1 = NSLayoutConstraint()
var verticalConstraintOuter = NSLayoutConstraint()

    horizontalConstraint1 = v.leadingAnchor.constraint(equalTo: (v.superview?.leadingAnchor)!,constant:10)
    horizontalConstraint2 = v.trailingAnchor.constraint(equalTo: (v.superview?.trailingAnchor)!,constant:-10)
      if self.prevView == nil{
           verticalConstraint1 = v.topAnchor.constraint(equalTo: (v.superview?.topAnchor)!, constant: 0)
           NSLayoutConstraint.activate([horizontalConstraint1,horizontalConstraint2,verticalConstraint1 ]) 
            }else {
                 if let pv = self.prevView as? UIView {
                  verticalConstraint1 = v.topAnchor.constraint(equalTo: (pv.bottomAnchor), constant: 10)
                  verticalConstraintOuter = v.topAnchor.constraint(equalTo: (v.superview?.topAnchor)!,constant:10 )
                  verticalConstraintOuter.priority = UILayoutPriority(rawValue: 800)
                  NSLayoutConstraint.activate([horizontalConstraint1,horizontalConstraint2,verticalConstraint1,verticalConstraintOuter])
                        }
     }
       self.prevView = v 

Your technique is wrong. 您的技术是错误的。 You do not add a "secondary top constraint to each view to account for the deleting". 您没有在每个视图中添加“次要顶部约束来说明删除”。 When you remove a view, you completely remove the existing constraints and create a whole new set of constraints. 删除视图时,您将完全删除现有约束并创建一组全新的约束。

Alternatively, use UIStackView, which does the work for you: you set an arranged view's isHidden to true and the stack view rewrites the constraints of its arranged views. 另外,也可以使用UIStackView为您完成工作:将已安排视图的isHiddentrue ,然后堆栈视图将重写其已安排视图的约束。

In your case, an even simpler technique would be to make your interface be a UITableView. 在您的情况下,更简单的方法是使您的界面成为UITableView。 Now all you have to do is remove a row of the table. 现在,您要做的就是删除表的一行。 The notion of "delete this row" is basically built into a table view; “删除此行”的概念基本上内置于表格视图中; you get it almost for free (including a Delete button, though no law says you have to use that). 您几乎可以免费获得它(包括删除按钮,尽管没有法律规定您必须使用它)。

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

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