@iFamily
Expert in Server-Side-Swift and iOS development

Как программно правильно задать auto-layout constraints?

Добрый день!

Мне необходимо поверх всего в Navigation Controller'e наложить картинку 250x127 так, чтобы она располагалась всегда в правом верхнем углу.

Пытаюсь следующим образом:
UIImageView *soldImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"stampik"]];
    [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]];


Прошу помощи, так как различные вариации по советам из гугла не срабатывают.
  • Вопрос задан
  • 3577 просмотров
Решения вопроса 1
@iFamily Автор вопроса
Expert in Server-Side-Swift and iOS development
Нашел решение сам:
[soldImageView setTranslatesAutoresizingMaskIntoConstraints:NO];

Не очень-то продвинутая публика здесь... написал сюда только из-за того, что stackoverflow в read-only.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы