I
I
iMike2014-05-16 10:11:53
Objective-C
iMike, 2014-05-16 10:11:53

How to set auto-layout constraints programmatically?

Good afternoon!
I need to superimpose a 250x127 picture on top of everything in the Navigation Controller so that it is always located in the upper right corner.
I'm trying like this:

UIImageView *soldImageView = ;
    [self.navigationController.view addSubview:soldImageView];
    [self.navigationController.view bringSubviewToFront:soldImageView];
    [self.navigationController.view addConstraint:[NSLayoutConstraint constraintWithItem:soldImageView
                                                                               attribute:NSLayoutAttributeTrailing
                                                                               relatedBy:NSLayoutRelationEqual
                                                                                  toItem:self.navigationController.view
                                                                               attribute:NSLayoutAttributeRight
                                                                              multiplier:1.0
                                                                                constant:20.0]];
    [self.navigationController.view addConstraint:[NSLayoutConstraint constraintWithItem:soldImageView
                                                                               attribute:NSLayoutAttributeTop
                                                                               relatedBy:NSLayoutRelationEqual
                                                                                  toItem:self.navigationController.view
                                                                               attribute:NSLayoutAttributeTop
                                                                              multiplier:1.0
                                                                                constant:70.0]];
    [self.navigationController.view addConstraint:[NSLayoutConstraint constraintWithItem:soldImageView
                                                                               attribute:NSLayoutAttributeWidth
                                                                               relatedBy:NSLayoutRelationEqual
                                                                                  toItem:nil
                                                                               attribute:NSLayoutAttributeNotAnAttribute
                                                                              multiplier:1.0
                                                                                constant:250.0]];
    [self.navigationController.view addConstraint:[NSLayoutConstraint constraintWithItem:soldImageView
                                                                               attribute:NSLayoutAttributeHeight
                                                                               relatedBy:NSLayoutRelationEqual
                                                                                  toItem:nil
                                                                               attribute:NSLayoutAttributeNotAnAttribute
                                                                              multiplier:1.0
                                                                                constant:127.0]];

I ask for help, as various variations on the advice from Google do not work.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
iMike, 2014-05-16
@iFamily

Found the solution myself:
Not very advanced audience here... only posted here because stackoverflow is read-only.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question