如何在iOS 6中删除导航栏标题的默认阴影效果

时间:2013-12-06 10:00:08

标签: ios ios6 shadow navigationbar

解决方案

现在基于@visualication提供的想法,以下代码(将其放在application:didFinishLaunchingWithOptions:中)将解决问题:

[[[self navigationController] navigationBar] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], UITextAttributeTextShadowColor:[UIColor whiteColor], UITextAttributeTextShadowOffset:[NSValue valueWithUIOffset:UIOffsetMake(0, 0)]}];

现在在iOS 6上截图:

enter image description here

原帖

在iOS 6上看一下这个截图:

enter image description here

标题上有一些黑色阴影,我没有编码阴影,我只给出了背景图像,320点宽,44点高,有一种红色。

    /// Create background image for navigation bar in iOS 6 or prior programmatically
    CGRect rect = CGRectMake(0.0f, 0.0f, screenBoundsRect.size.width, 44.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [[UIColor colorWithRed:201/255.f green:32/255.f blue:38/255.f alpha:1.00] CGColor]); // a red color
    CGContextFillRect(context, rect);
    UIImage *navigationBarBackgroundImageForiOS6 = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [[[self navigationController] navigationBar] setBackgroundImage:navigationBarBackgroundImageForiOS6 forBarMetrics:UIBarMetricsDefault]; // UIBarMetricsDefault is portrait in iPhone

使用以下代码将标题设置为导航栏:

    /// Set the title
    [[[self navigationController] topViewController] setTitle:@"dynamiclc2"];

上面的标题设置代码在iOS 6中给出了阴影但在iOS 7中没有阴影:

enter image description here

我希望iOS 6中的导航栏标题显示与iOS 7版本相同(或几乎相同)。

1 个答案:

答案 0 :(得分:3)

你可以设置阴影lIkE:

[[UINavigationBar appearance] setTitleTextAttributes:
 [NSDictionary dictionaryWithObjectsAndKeys:
  [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0],
  UITextAttributeTextShadowColor,
  [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
  UITextAttributeTextShadowOffset,
  nil]];