ios - 我应该什么时候设计 UINavigationBar?

标签 ios objective-c uinavigationbar

我有一个 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/

相关文章:

ios - Safari 无法打开页面,因为地址无效

ios - swift 2.1 无法将 anyobject 类型的值转换为预期的参数类型 nslayoutconstraint

ios - 为什么我的 UICollectionView 在单击单元格时会向上滚动?

iphone - 获取时间间隔并在 Objective-C 中播放声音

iOS 11.0 UITextField secureTextEntry 不起作用?

objective-c - MetalKit - 如何使用(MTKTextureLoader 方法)newTextureWithContentsOfURL 的选项?

objective-c - 如何创建明天早上 8 点的日期对象

ios - 更改导航栏高度

ios - 需要 wantsFullScreenLayout 行为与 iOS 7 上的非半透明栏

ios - 自定义 UINavigationBar 的背景图片 - Swift 3.0