5

Я хочу пользовательский UIView в моем UINavigationBar между левым и правым BarButtonItem, но как можно шире. По этой причине я добавил UIView в IB к NavigationBar. При отключении автоотключения все работает так, как ожидалось, с помощью авторезистирующих масок. Но в моем раскадровке с включенным автозапуском я просто не могу заставить его работать. Не похоже, что я могу установить любые ограничения в IB для titleView. Если я поворачиваю свое устройство в альбомный режим, UIView имеет одинаковую ширину. Что мне нужно сделать, чтобы titleView заполнил пробел между моими UIBarButtonItems с включенным автозапуском?Autoresize titleView в NavigationBar с автозапуском

Спасибо за любую помощь

LINARD

ответ

4

Я решил проблему следующим образом в моем коде:

- (void)willAnimateRotationToInterfaceOrientation:(__unused UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration]; 

    CGSize navigationBarSize = self.navigationController.navigationBar.frame.size; 
    UIView *titleView = self.navigationItem.titleView; 
    CGRect titleViewFrame = titleView.frame; 
    titleViewFrame.size = navigationBarSize; 
    self.navigationItem.titleView.frame = titleViewFrame; 
} 

я не нашел другое решение (с автоматическим изменением размеров), но я открытые для новых и/или более эффективных решений

Linard

-4

Вы можете добавить ограничения в код.

В viewDidLoad, вы можете сделать что-то вроде этого:

UIView *titleView = [[UIView alloc] initWithFrame:CGRectZero]; 
self.navigationItem.titleView = titleView; 
UIView *titleViewSuperview = titleView.superview; 
titleView.translatesAutoresizingMaskIntoConstraints = NO; 
[titleViewSuperview addConstraint:[NSLayoutConstraint constraintWithItem:titleView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:titleViewSuperview attribute:NSLayoutAttributeLeading multiplier:1 constant:0]]; // leading 
[titleViewSuperview addConstraint:[NSLayoutConstraint constraintWithItem:titleView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:titleViewSuperview attribute:NSLayoutAttributeTop multiplier:1 constant:0]]; // top 
[titleViewSuperview addConstraint:[NSLayoutConstraint constraintWithItem:titleViewSuperview attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:titleView attribute:NSLayoutAttributeWidth multiplier:1 constant:0]]; // width 
[titleViewSuperview addConstraint:[NSLayoutConstraint constraintWithItem:titleViewSuperview attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:titleView attribute:NSLayoutAttributeHeight multiplier:1 constant:0]]; // height 
+0

Спасибо за ваше предложение. Я попробую, как только у меня будет время ... –

+0

Я получаю сбой при ограничении ширины, потому что titleSuperView имеет значение NULL. Кто-нибудь еще не повезло? – djibouti33

+6

В iOS 7, по крайней мере, вы не можете модифицировать ограничения для UINavigationBar, управляемые контроллером – Andy

Смежные вопросы