0

Привет!

Имеется экран с segmented control, который имеет 2 состояния. На картинке все видно:

alt text

Включен Autolayout. Так вот, как прописать constraints для верхней границы UITableView (отмечен желтым цветом) чтобы для первого состояния, таблица привязывалась к navigation bar, а во втором состоянии - к UIView(вьюшка с элементами Откуда-Куда).

Пробовал для каждого состояния вручную прописать tableView.frame = CGRectMake... , но такое не работает.

2 ответа 2

0

Вот тут есть хороший пример: Creating UIViews programmatically with Auto Layout

Since Xcode 4.0 I have exclusively developed views in my iOS apps with Interface Builder (IB). First with xibs and recently with Storyboards. I'm a fan of IB and have successfully released several apps that use Auto Layout in the often criticized Xcode 4.x version of IB. I have been able to avoid the most common IB compliant, merge issues, with good team communication and a bit of luck.

1
  • Спасибо за ответ, но не получается:( Посмотрите пожалуйста внизу, дополнение к посту.
    – michilly
    25 июн 2014 в 8:19
0

Попробовал следующее: при загрузке вьюшки в зависимости от состояния создаю constraints:

- (void)setCityConstraints {
    NSLog(@"set city constraints");
    tView.translatesAutoresizingMaskIntoConstraints = NO;
    NSLayoutConstraint *myConstraint =[NSLayoutConstraint
                                       constraintWithItem:self.tView
                                       attribute:NSLayoutAttributeTop
                                       relatedBy:NSLayoutRelationEqual
                                       toItem:navBar
                                       attribute:NSLayoutAttributeBottom
                                       multiplier:1.0
                                       constant:0.0];

    [self.view addConstraint:myConstraint];
}

- (void)setInterCityConstraints {
    NSLog(@"set intercity constraints");
    tView.translatesAutoresizingMaskIntoConstraints = NO;
    NSLayoutConstraint *myConstraint =[NSLayoutConstraint
                                       constraintWithItem:self.tView
                                       attribute:NSLayoutAttributeTop
                                       relatedBy:NSLayoutRelationEqual
                                       toItem:navBar
                                       attribute:NSLayoutAttributeBottom
                                       multiplier:1.0
                                       constant:50.0];

    [self.view addConstraint:myConstraint];
}

При первой загрузке все хорошо работает, но если я начну изменять состояния вьюшки, изменяя segmentedControl - то на второй форме tableView налезает на UIView (там где кнопки Откуда и Куда).

- (IBAction)orderTypeChanged:(id)sender {
    if (orderTypeControl.selectedSegmentIndex == 0) {
        [self showCity];
        [self setCityConstraints];
    } else {
        [self showIntercity];
        [self setInterCityConstraints];
    }
}
4
  • Попробуйте добавить consraints не к navbar, а к topLayoutGuide, то есть [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topLayoutGuide attribute:NSLayoutAttributeTop // может и bottom, не уверен, нужно поиграться с этой штукой multiplier:1.0 constant:50.0];
    – iFreeman
    26 июн 2014 в 16:50
  • У вас нет возможности сделать это в xib? На своем опыте, обычно это решение
    – iFreeman
    26 июн 2014 в 16:52
  • еще опишите пожалуйста, не выводятся ли в логи сообщения о неверных constraints?
    – iFreeman
    26 июн 2014 в 16:54
  • Логи выводятся, что как раз эти констрейны неправильные и будут брейкнуты. Поэтому пришлось сделать костыли: вывожу инфу на 2 UITableView: своя таблица для каждого состояния
    – michilly
    27 июн 2014 в 2:16

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.