LoginSignup
15
14

More than 5 years have passed since last update.

UINavigationBarの背景画像を設定する方法

Last updated at Posted at 2012-05-30

iOSのバージョンによって色々やり方があるように思えますが、とても簡単な方法が以下。

example.m

#import <QuartzCore/QuartzCore.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIViewController *vc = [[UIViewController alloc] initWithNibName:@"ExampleView" bundle:nil];
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
    nc.navigationBar.tintColor = [UIColor blackColor];//画像の色に合う色にした方が良いと思う

    NSString *currentVersion = [[UIDevice currentDevice] systemVersion];
    if ([currentSystemVersion compare:@"5.0" options:NSNumericSearch] == NSOrderedAscending) {
        // iOS5.0以前の場合
        UIImageView *navBg = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"bg-nav.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:1]];
        navBg.layer.zPosition = -FLT_MAX;
        [nc.navigationBar insertSubView:navBg atIndex:0];
    } else if (([currentSystemVersion compare:@"5.0" options:NSNumericSearch] == NSOrderedSame) || ([currentSystemVersion compare:@"5.0" options:NSNumericSearch] == NSOrderedDescending)) {
        // iOS5.0以降の場合
        nc.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg-nav.png"] forBarMetrics:UIBarMetricsDefault];
    }

}

※やはりiOSのバージョンで切り分けるやり方が無難のようです。ボタンとかが隠れちゃう現象も上記のやり方
なら大丈夫です。

15
14
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
14