0

Good day, friends!

I'm currently developing my first app and I can't get through one issue with auto layout. I have to set up constraints between two buttons. But they have to be changing depending on the resolution of display: not a constant offset.

As far as I understand, I can't do it in IB, so I've built constraints programmatically with code like this:

[self.view addConstraints:[NSLayoutConstraint
                               constraintsWithVisualFormat:@"H:|-horizontalOffset-[button1(>=102)]-buttonsInterval-[button2(>=102)]-horizontalOffset-|"
                               options:0
                               metrics:metrics
                               views:buttons]];

It works fine if I create buttons in code, but it doesn't work if buttons are created in storyboard. I do the update here:

- (void)updateViewConstraints
{
    [super updateViewConstraints];
    [self setMyConstraintsForScreen:self.view];
}

But there always are some conflicts with previously created constraints for this buttons - either xcode creates them by itself or I set them up manually in IB.

When I try to delete all constraints from the root view, I have this error:

'The layout constraints still need update after sending -updateViewConstraints to <ViewController: 0x7ffa6a5497c0>.
ViewController or one of its superclasses may have overridden -updateViewConstraints without calling super or sending -updateConstraints to the view. Or, something may have dirtied layout constraints in the middle of updating them.  Both are programming errors.'

I do call [super updateViewConstraints] though... I don't send updateViewConstraints to any particular view either.

Does anyone have a solution? Basically I need to do a simple thing: set up variable constraints for the view built in storyboard.

Thanks!

1
  • instead of setting the constraint between the two buttons, can you set the constraints to the screen edge while it is in portrait mode? That way when you rotate to landscape they will move farther apart and closer together in portrait mode. Commented Jan 8, 2015 at 21:43

1 Answer 1

1

You define an IBOutlet of type NSLayoutConstraint in your class (code) then you connect it in IB with your constraint definition.

Then in your code you can change the constraint for example with

constraint.constant = 123;
[view layoutIfNeeded];
Sign up to request clarification or add additional context in comments.

Comments

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.