Ios 我应该什么时候设计我的UINavigationBar?

Ios 我应该什么时候设计我的UINavigationBar?,ios,objective-c,uinavigationbar,Ios,Objective C,Uinavigationbar,我有一个ViewController,它有一个设计UINavigationBar的方法,一个从ViewController继承并调用其设计方法的SubViewController,一个ShowingViewController和一个从SubViewController继承的SecondViewController ShowingViewController是UINavigationController的根控制器,并执行到第二个ViewController的“显示”顺序。 它们都具有NSStrin

我有一个ViewController,它有一个设计UINavigationBar的方法,一个从ViewController继承并调用其设计方法的SubViewController,一个ShowingViewController和一个从SubViewController继承的SecondViewController

ShowingViewController是UINavigationController的根控制器,并执行到第二个ViewController的“显示”顺序。 它们都具有NSString属性“presentingProperty”。 ViewController为显示属性字符串的导航栏设置自定义titelView

我的问题是:我应该在哪里调用ViewController的设计方法

当我在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];
}

谢谢您的帮助。

您应该在以下位置设置导航栏的标题视图:

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

    <your titleView code here>
}
-(void)视图将显示:(BOOL)动画{
[超级视图将显示:动画];
}
第一次加载viewDidLoad后调用此函数,稍后从导航堆栈中弹出当前ViewController之上的其他ViewController时调用此函数。在以下文档中可以找到ViewController生命周期的良好状态图:

在样式方面,还可以使用以下代码设置标题视图:

self.navigationItem.titleView = <your-view>;
self.navigationItem.titleView=;
您可以试试这个

- (void)viewDidLoad
{
      self.navigationItem.titleView = <your titleView>
}
- (void)viewWillLayoutSubviews
{
     <your titleView>.frame = CGRectMake(....)
}
-(void)viewDidLoad
{
self.navigationItem.titleView=
}
-(无效)视图将布局子视图
{
.frame=CGRectMake(..)
}