2017-10-20 85 views
0

我有一個視圖控制器具有設計UINavigationBar的,它從視圖控制器繼承並調用它的設計方法和ShowingViewController和SecondViewController從SubViewController繼承SubViewController的方法。什麼時候應該設計我的UINavigationBar?

ShowingViewController是UINavigationController的根控制器,並執行「顯示」塞格到SecondViewController。 它們都具有NSString屬性「presentingProperty」。 ViewController將自定義titelView設置爲顯示屬性字符串的導航欄。

我的問題:我應該在哪裏調用視圖控制器的設計方法?

當我把它稱爲viewWillLoad,當我切換到SecondViewController因爲該方法改變了「老」 ShowingViewController的導航欄,我將無法正常工作。

當我把它在viewDidLoad中,用戶將看到之前的非故意導航欄。

我的設計方法的代碼:

if (self.navigationController.navigationBar) { 
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 21)]; 
    NSMutableAttributedString *titleText = [[NSMutableAttributedString alloc] initWithString:self.presentingProperty]; 
    [titleText addAttribute: NSForegroundColorAttributeName value: [UIColor whiteColor] range: NSMakeRange(0, self.presentingProperty.length)]; 
    [titleText addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Avenir-Heavy" size:21] range:NSMakeRange(0, self.presentingProperty.length)]; 
    [titleLabel setTextAlignment:NSTextAlignmentCenter]; 
    [titleLabel setAttributedText: titleText]; 
    [self.navigationController.navigationBar.topItem setTitleView:titleLabel]; 
} 

謝謝您的幫助。

回答

0

您應該建立於titleview了的導航欄在:

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear: animated]; 

    <your titleView code here> 
} 

這就是所謂的後viewDidLoad中上的第一次,當其他ViewControllers高於當前一個被彈出導航後來被稱爲疊加。該視圖控制器生命週期的良好狀態圖可以在文檔在https://developer.apple.com/documentation/uikit/uiviewcontroller#1652793

被發現在一個風格的時候,你還可以設置於titleview與此代碼:

self.navigationItem.titleView = <your-view>; 
1

你可以試試這個。

- (void)viewDidLoad 
{ 
     self.navigationItem.titleView = <your titleView> 
} 
- (void)viewWillLayoutSubviews 
{ 
    <your titleView>.frame = CGRectMake(....) 
} 
相關問題