5

Chcę niestandardowego UIView w moim UINavigationBar między lewym i prawym BarButtonItem, ale tak szeroki, jak to możliwe. Z tego powodu dodałem UIView w IB do NavigationBar. Przy wyłączonym autouzupełnianiu wszystko działa zgodnie z oczekiwaniami dzięki maskom automatycznego zapisywania. Ale w moim scenopisie z włączonym autolayout po prostu nie mogę go uruchomić. Wygląda na to, że nie mogę ustawić żadnych ograniczeń w IB dla titleView. Jeśli obrócę urządzenie do trybu poziomego, UIView ma nadal tę samą szerokość. Co muszę zrobić, aby tytułView wypełniał przestrzeń między moimi UIBarButtonItems z włączonym autolayout?Autoresize titleView w NavigationBar z autolayout

Dziękuję za wszelką pomoc

Linard

Odpowiedz

4

I rozwiązać problem w następujący sposób w moim kodu:

- (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; 
} 

nie znalazłem inne rozwiązanie (z automatyczną zmianą rozmiaru), ale otwarte na nowe i/lub lepsze rozwiązania

Linard

-4

Możesz dodać ograniczenia w kodzie.

W viewDidLoad, można zrobić coś takiego:

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

Dziękuję za sugestię. Spróbuję go jak tylko będę mieć czas ... –

+0

Występuje awaria na ograniczeniu szerokości, ponieważ titleSuperView ma wartość null. Czy ktokolwiek miał szczęście? – djibouti33

+6

Co najmniej w systemie iOS 7 nie można modyfikować ograniczeń dla UINavigationBar zarządzanego przez kontroler – Andy

Powiązane problemy