首页 文章

重绘UINavigationBar以反映 Headers 文本颜色更改

提问于
浏览
2

我正在尝试使用UINavigationBar Headers 文本颜色作为用户指示我的应用程序是否与外部附件有连接,例如红色表示断开连接,绿色表示连接 .

通过使用setTitleTextAttributes方法然后调用[self.navigationBar setNeedsDisplay],在连接状态更改时更新UINavigationBar以获得正确的 Headers 文本颜色没有问题 . 但是,更改并未立即反映出来 .

我还使用[[UINavigationBar外观] setTitleTextAttributes:...],以便将来创建的任何新UINavigationBars都具有正确的值 . 如果我导航到新视图,导航栏 Headers 是正确的颜色 .

如何使用更新的 Headers 颜色立即强制重新绘制UINavigationBar?

2 回答

  • 0

    我想我刚遇到同样的问题 . 这是一种更容易解决此问题的方法 . 假设 UINavigationBar 是标准 UINavigationController 的栏,您只需要重新分配正在显示的文本 . 这是你的控制器的 title 属性 . 诀窍是,您需要更改其值才能使其正常工作 .

    self.navigationController.navigationBar.titleTextAttributes = @{...}
    
    self.title = nil;
    self.title = @"##Actual Title##";
    
  • 2

    我使用以下代码在iPad模拟器上工作:

    // update the title text color
    NSDictionary *navBarTitleOptions = [NSDictionary 
            dictionaryWithObjectsAndKeys:newColor, UITextAttributeTextColor, nil];
    [self.navigationBar setTitleTextAttributes:navBarTitleOptions];
    
    // set and clear the title view in order to force the title to be 
    // redrawn in the correct color.
    [self.navigationBar.topItem setTitleView:[[UIView alloc] init]];
    [self.navigationBar setNeedsDisplay];
    [self.navigationBar.topItem setTitleView:nil];
    [self.navigationBar setNeedsDisplay];
    

    然而,这似乎不适用于iPhone / iPod模拟器 . 有什么想法吗?

相关问题