2

In a view I'm inserting a new UIButton, As I have been using AutoLayout, I need to get constraint of the view below which I'm going to insert button. How do I get the NSLayoutConstraint programmatically, so that I can delete the old constraint create new one afterwards. Thanx.

Edit:enter image description here

with ref to diagram, I need to insert B3 between B1 and B2, So I need to delete the already set fixed space constraint between B1 and B2, and I need to connect B3 bottom to B2's top and B3's top to B1's bottom.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Newbee
  • 3,231
  • 7
  • 42
  • 74
  • Are you certain you need to delete the constraint? Can you show a diagram/code of what the constraints are. – Fogmeister Jul 10 '13 at 09:13
  • Cool, OK yeah you'll need to delete. OK, gimme a sec... actually, can't answer now. Take a look on the github project linked from here though http://www.oliverfoggin.com/an-auto-layout-experiment/ I do this a lot. – Fogmeister Jul 10 '13 at 09:25
  • okay I'll check thank you... – Newbee Jul 10 '13 at 09:35
  • I don't get what you need: all the constraints to draw this ? Or something else ? – The Windwaker Jul 24 '14 at 12:05
  • I believe the question is: *How do I get the NSLayoutConstraint programmatically, so that I can delete the old constraint create new one afterwards.*, which, in other words, means: *How can I get a hold of the old constraint*. – SwiftArchitect Aug 26 '15 at 13:27

2 Answers2

2

I need to delete the already set fixed space constraint between B1 and B2

Finding a constraint programmatically

You can either search for B1 in the view constraints, or keep a reference to the NSLayoutConstraint when you created it. Searching the constraints for B1 is probably less efficient: all the relationship constraints (view to another view) are part of the enclosing superview. Assuming you have a handle to B1, you can list all B1 constraints in its superview like so:

// Searching all relationship constraints involving b1
for item in self.view.constraints() {
    if let constraint = item as? NSLayoutConstraint {
        if let button = constraint.firstItem as? UIButton {
            if button == b1 {
                println("firstItem found: \(constraint)")
            }
        }
        if let button = constraint.secondItem as? UIButton {
            if button == b1 {
                println("secondItem found: \(constraint)")
            }
        }
    }
}

Remembering a constraint

By far, the cleaner approach is to keep a reference to the NSLayoutConstraint you want to later modify or delete. To do this in a Storyboard, create the constraints you need.

enter image description here

Then control-drag a reference directly into your source file to manipulate them later.

enter image description here

SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
0

You can turning off the default constraints applied to views as below. apply below code while you first Created Button programmatic.

B1.translatesAutoresizingMaskIntoConstraints = NO;
B2.translatesAutoresizingMaskIntoConstraints = NO;
B3.translatesAutoresizingMaskIntoConstraints = NO;

After adding all button in to your View using AddSubView set your Constrain again as par your need for example below.

// Center the middle one vertically
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:B2 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];

hope it's help you i found best answer in Stack Overflew from Evenly space multiple views within a container view Al the very best.

Community
  • 1
  • 1
Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144