0

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

The fine version where I dont add the secondary priority 800 top anchor

Once I add a secondary top constraint to each view to account for the deleting then I get the following....

enter image description here

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 
3
  • I agree with Matt’s suggestions of better approaches: The other advantage of table view (or collection view) is that you’re not limiting your number of reminders (or whatever) to what can fit in the screen at one time. Commented Feb 9, 2018 at 17:29
  • HI @Rob the parent is sitting in a scroll view so there is enough room..I will try to add things to the stackview and see how that works... Commented Feb 9, 2018 at 18:22
  • Cool. Nonetheless, table view is more efficient way of presenting long lists of items. That's what it was designed for. And it gets you out of this constraints and stack view silliness. Commented Feb 9, 2018 at 18:37

1 Answer 1

2

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.

In your case, an even simpler technique would be to make your interface be a 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).

Sign up to request clarification or add additional context in comments.

3 Comments

I tried the UIStackView but upon removal of the views inside it drove the UIStack view crazy...it would not respect the vertical spacing to the element above..etc etc
There are easy ways to fix that. But I also gave you two other suggestions. In any case what you were doing was wrong, and I explained why.
I had a stray constraint from my other attempts...seems to be working ok now with UIstack view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.