5

我正在尝试在我的表格视图中为导航栏设置自定义阴影图像,但它只显示在某些视图中。我创建了一个超类来为我的表格视图设置样式。

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Set navigation bar background
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbarbackground.png"] forBarMetrics:UIBarMetricsDefault];

    // Set navigation bar shadow imag
    [self.navigationController.navigationBar setShadowImage:[UIImage imageNamed:@"navigationbarshadow.png"]];

在我启动应用程序时看到的视图中,没有显示阴影。但是当我触摸导航栏中的 [+] 按钮以打开我的“添加新项目”表视图时,它确实显示了一个阴影。

有人可以在这里指出我正确的方向吗?

4

3 回答 3

9

you need to set custom backgroudImage for UINavigationBar, then the shadowImage can work.

于 2015-01-03T08:59:05.707 回答
7

外观代理应该可以工作。

只需在启动时在某个地方(例如在您的 AppDelegate 中)调用它。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ 
    [self customizeAppearance];
    return YES;
}

- (void) customizeAppearance 
{
    // Set the background image for *all* UINavigationBars
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navigationbarbackground"] forBarMetrics:UIBarMetricsDefault];

    // Set the shadow image for *all* UINavigationBars
    [[UINavigationBar appearance] setShadowImage:[UIImage imageNamed:@"navigationbarshadow.png"]];  

    //add other appearance stuff here...
}

但是,如果您创建一个包含多个 UINavigationController 的故事板和一堆 segue 的推送导航控制器,您可能会得到一个损坏的视图控制器结构,这可能是这里的问题。

Another possible issue might be the Clip Subviews option of a Navigation Bar somewhere in your nib file or you storyboard. Make sure it is turned off if you want the shadow (image)!

ClipSubviews

By the way, if you use imageNamed you don't need to include the file extension.

于 2013-02-02T12:57:25.913 回答
0

试试这个 !

[[UINavigationBar appearance] setShadowImage:[UIImage imageNamed:@"navbar-iphone.png"]];
于 2013-02-01T12:12:40.343 回答