簡體   English   中英

iOS 7-導航欄項目文本顏色

[英]iOS 7 - navigation bar item text color

我在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];
//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;
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];
[[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]];

在passcodePopover的viewdidLoad方法中,您將要使用此代碼。

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

如果彈出窗口中沒有NavigationController,則必須稍作更改代碼,也可能不想使用[UIColor whiteColor]。 而是替換您自己選擇的顏色。

在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 
    }

而是創建一個類別:

@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];
}

其中kAppFontBoldkAppFontBoldSize是宏。

您可以使用它。 它可能會幫助您。在這里,我為兩種狀態設置了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 +。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM