2017-10-20 4 views
0

私はUINavigationBarを設計するメソッドを持つViewControllerを持っています.UnavigationBarは、ViewControllerを継承し、その設計メソッドと、SubViewControllerを継承するDisplaysViewControllerとSecondViewControllerを呼び出します。私のUINavigationBarはいつデザインするのですか?

ShowViewControllerはUINavigationControllerのルートコントローラであり、SecondViewControllerに対する「表示」セグメントを実行します。 これらは両方ともNSStringプロパティ "presentingProperty"を持っています。 ViewControllerは、プロパティの文字列を表示するナビゲーションバーにカスタムtitelViewを設定します。

私の質問:ViewControllerの設計方法はどこで呼びますか?

viewWillLoadで呼び出すと、メソッドが "古い" ShowingViewControllerのNavigationBarを変更するため、SecondViewControllerに切り替えると機能しません。

私はそれをviewDidLoadで呼び出すと、前に無差別のNavigationBarが表示されます。私の設計方法の

コード:

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

あなたにはnavigationBarためtitleViewを設定する必要があります。

これは のviewDidLoad後の最初の時間に呼び出され、現在の1以上の他のViewControllersナビゲーションをオフにポップされたときに、後に呼び出され
- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear: animated]; 

    <your titleView code here> 
} 

スタック。 ViewControllerのライフサイクルの良い状態図は、スタイルのポイントに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(....) 
} 
関連する問題