您的位置:首页 > 产品设计 > UI/UE

UINavigationBar添加背景图片

2020-05-11 04:07 966 查看

代码修改如下:

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

 

    _rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:[NSBundle mainBundle]];

    _nav = [[UINavigationController alloc] initWithRootViewController:_rootViewController];

 

    UIImage *backgroundImage = [UIImage imageNamed:@"NaviBackground.png"];

    [self setNavigationBarBackgroundImage:backgroundImage];

 

    [self.window setRootViewController:_nav];

    [self.window makeKeyAndVisible];

 

    returnYES;

}

 

- (void)setNavigationBarBackgroundImage:(UIImage *)backgroundImage

{

    UINavigationBar *navBar = [_nav navigationBar];

    if ([navBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {

        [navBar setBackgroundImage:backgroundImage forBarMetrics:UIBarMetricsDefault];

    }

    else {

        UIImageView *imageView = (UIImageView *)[navBar viewWithTag:kSCNavBarImageTag];

        if (imageView == nil) {

            imageView = [[UIImageView alloc] initWithImage:backgroundImage];

            [imageView setTag:kSCNavBarImageTag];

            [navBar insertSubview:imageView atIndex:0];

            [imageView release];

        }

    }

}

 

参考:

Adding a Background Image to UINavigationBar

An Introduction To UINavigationController

学习笔记:UINavigationController使用详解

转载于:https://www.cnblogs.com/StarMud/archive/2012/11/30/2796397.html

a2614555 原创文章 0获赞 0访问量 209 关注 私信
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: