【问题标题】:iOS 7 - navigation bar item text coloriOS 7 - 导航栏项目文本颜色
【发布时间】:2013-12-23 04:26:41
【问题描述】:

我在 iOS7 上遇到导航栏项目的文本颜色问题。

在我展示一个视图控制器之前,一切都很好,从这个时候开始,所有导航栏项目的文本颜色都是灰色的。您对这个问题有任何经验吗?

用于呈现VC的代码:

[passcodePopover presentPopoverFromRect:CGRectMake([UIScreen mainScreen].bounds.size.width/2 ,currentWindow.frame.size.height/2,1,1)
                                     inView:currentWindow
                   permittedArrowDirections:0 animated:NO];[passcodePopover presentPopoverFromRect:CGRectMake([UIScreen mainScreen].bounds.size.width/2 ,currentWindow.frame.size.height/2,1,1)
                                     inView:currentWindow
                   permittedArrowDirections:0 animated:NO];

【问题讨论】:

  • 我找到了根本原因,当呈现一个视图控制器时,会改变应用程序的色调属性。不知道为什么

标签: ios ios7 uipopovercontroller uinavigationitem presentmodalviewcontroller


【解决方案1】:
//call the below code in ViewDidLoad /It works fine in iOS7

UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0, 320, 100)];
navLabel.backgroundColor = [UIColor clearColor];
navLabel.text = @"Event Details";
navLabel.textColor = [UIColor whiteColor];
navLabel.shadowOffset = CGSizeMake(0, -2);
[navLabel setFont:[UIFont boldSystemFontOfSize:16]];
navLabel.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = navLabel;

【讨论】:

    【解决方案2】:
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero] ;
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont boldSystemFontOfSize:20.0]; // font size
    label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
    label.textAlignment = NSTextAlignmentCenter;
    
    label.textColor = [UIColor yellowColor]; // change this color
    self.navigationItem.titleView = label;
    label.text = NSLocalizedString(@"Product Info.", @""); // lable name
    [label sizeToFit];
    

    【讨论】:

      【解决方案3】:
      [[UINavigationBar appearance] setTitleTextAttributes:
         [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],
         NSForegroundColorAttributeName,[UIColor whiteColor],
         NSForegroundColorAttributeName,[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
         NSForegroundColorAttributeName,[UIFont fontWithName:@"Arial-Bold" size:0.0],
         NSFontAttributeName,nil]];
      

      【讨论】:

        【解决方案4】:

        在 passcodePopover 的 viewdidLoad 方法中你会想要使用这个代码。

        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
        NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil];
        [self.navigationController.navigationBar setTitleTextAttributes:textTitleOptions];
        

        如果你的popover 中没有navigationController,你将不得不稍微改变代码,你也可能不想使用[UIColor whiteColor]。而是替换您自己选择的颜色。

        【讨论】:

          【解决方案5】:

          在 viewdid 加载函数中使用下面的代码

            if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
          
                  // Load resources for iOS 6.1 or earlier
                   [[UINavigationBar appearance]setTintColor:NavigationColor];
              } else
               {
                   [[UINavigationBar appearance]setTintColor:[UIColor whiteColor]]; // it set color of bar button item text 
              }
          

          【讨论】:

          • 用于检查操作系统版本的技术取决于您的目标平台,如下所示: 要在运行时检查 iOS 版本,请使用如下代码:NSString *osVersion = [[UIDevice currentDevice] systemVersion]; 检查操作系统版本X 在运行时,使用格式塔函数和系统版本选择器常量。
          【解决方案6】:

          改为创建一个类别:

          @implementation UINavigationBar (custom)
          - (void)setCustomNavigationBar: (NSString *)_strTitle
          {
              [self setBackgroundImage:[UIImage imageNamed:@"navigationbar"] forBarMetrics:UIBarMetricsDefault];
          
              UILabel *_lblTitle = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
              _lblTitle.text = _strTitle;
              _lblTitle.textAlignment = NSTextAlignmentCenter;
              [_lblTitle setBackgroundColor:[UIColor clearColor]];
              [_lblTitle setFont:[UIFont fontWithName:kAppFontBold size:kAppFontBoldSize]];
              [_lblTitle setTextColor:[UIColor navigationBarColor]];
              [[self topItem] setTitleView:_lblTitle];
          }
          

          其中kAppFontBold 和kAppFontBoldSize 是宏。

          【讨论】:

            【解决方案7】:

            你可以使用它。它可能会对您有所帮助.. 在这里,我为两个状态设置了 font and text color。

            [barItemForIndex setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                [UIFont systemFontOfSize:Tabbar_Item_Font_Size], NSFontAttributeName,
                                                                [UIColor darkGrayColor], NSForegroundColorAttributeName,
                                                                nil] forState:UIControlStateNormal];
            [barItemForIndex setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                [UIFont systemFontOfSize:Tabbar_Item_Font_Size], NSFontAttributeName,
                                                                [UIColor whiteColor], NSForegroundColorAttributeName,
                                                                nil] forState:UIControlStateSelected];
            

            注意: setTitleTextAttributes: 方法仅适用于 ios5.0+。

            【讨论】:

              猜你喜欢
              • 2013-10-02
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-09-26
              • 2015-05-14
              • 2017-01-13
              相关资源
              最近更新 更多