我有一个 ViewController,它具有设计 UINavigationBar 的方法,一个继承自 ViewController 并调用其设计方法的 SubViewController,以及一个继承自 SubViewController 的 ShowingViewController 和一个 SecondViewController。
ShowingViewController 是 UINavigationController 的根 Controller ,对 SecondViewController 执行“显示”Segue。 它们都具有 NSString 属性“presentingProperty”。 ViewController 将自定义 titelView 设置到显示属性字符串的导航栏。
我的问题:ViewController的设计方法应该在哪里调用?
当我在 viewWillLoad 中调用它时,当我切换到 SecondViewController 时我将无法工作,因为该方法更改了“旧”ShowingViewController 的 NavigationBar。
当我在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];
}
感谢您的帮助。
最佳答案
你可以试试这个。
- (void)viewDidLoad
{
self.navigationItem.titleView = <your titleView>
}
- (void)viewWillLayoutSubviews
{
<your titleView>.frame = CGRectMake(....)
}
关于ios - 我应该什么时候设计 UINavigationBar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46848093/