如何在每页的基础上更改导航栏的背景图像?

发布于 2024-09-15 00:51:12 字数 521 浏览 5 评论 0原文

我一直在寻找一种方法来更改 NavigationBar 的背景图像并在用户导航应用程序时控制 NavigationBar 的外观。

据我所知,更改背景图像的可接受方法是:

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawRect:(CGRect)rect {
     UIImage *image = [UIImage imageNamed: @"navbar.png"];
     [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

@end

但是,这会改变整个应用程序中 NavigationBar 的外观。当用户从一个视图导航到下一个视图时,如何更改 NavBar 的背景图像?

预先感谢您的帮助!

I've been looking around for a way to change the background image of my NavigationBar and control the appearance of my NavigationBar as the user navigates the app.

I understand that the accepted approach for changing the background image is :

@implementation UINavigationBar (UINavigationBarCategory)

- (void)drawRect:(CGRect)rect {
     UIImage *image = [UIImage imageNamed: @"navbar.png"];
     [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

@end

However, that changes the appearance of the NavigationBar throughout the whole app. How can I change the background image of the NavBar as the user navigates from one view to the next?

Thanks in advance for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

逆夏时光 2024-09-22 00:51:12

您需要在当前页面或当前要使用的适当图像的某个位置设置一些状态,可能在每个 viewWillAppear: 方法中。然后修改上面的 drawRect: 函数以引用该状态。

要使栏重新绘制,请在更新状态时调用 [myNavigationBar setNeedsDisplay]。这将导致调用drawRect

You need to set some state somewhere about the current page or currently appropriate image to use, probably in each of your viewWillAppear: methods. Then modify your drawRect: function above to reference that state.

To cause the bar to be redrawn, call [myNavigationBar setNeedsDisplay] when you update the state. This will cause drawRect to be invoked.

七堇年 2024-09-22 00:51:12

我真的建议阅读 Sebastian Celis 的教程,它确实对我有帮助 - http://sebastiancelis.com/2009/12/21/adding-background-image-uinavigationbar/

I'd really recommend reading a tutorial from Sebastian Celis, it really helped me - http://sebastiancelis.com/2009/12/21/adding-background-image-uinavigationbar/

过去的过去 2024-09-22 00:51:12

是否可以在导航栏上使用图像视图(添加)?

isn't it possible to use an image view (add) on the navigation bar?

就此别过 2024-09-22 00:51:12

我相信这是 iOS5 的第一个实际答案,主要问题是完成后背景图像的“删除”。好吧,只需保留现有图像并在完成后将其放回去即可。

@implementation MyViewController {
    UIImage *_defaultImage;
}

- (void)viewWillAppear:(BOOL)animated {   
    _defaultImage = [self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bar.png"] forBarMetrics:UIBarMetricsDefault];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self.navigationController.navigationBar setBackgroundImage:_defaultImage forBarMetrics:UIBarMetricsDefault];
}

I believe this to be the first actual answer to this for iOS5, the main problem being the "removal" of the background image once you are done. Well, just retain the existing image and put it back when you are done.

@implementation MyViewController {
    UIImage *_defaultImage;
}

- (void)viewWillAppear:(BOOL)animated {   
    _defaultImage = [self.navigationController.navigationBar backgroundImageForBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bar.png"] forBarMetrics:UIBarMetricsDefault];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self.navigationController.navigationBar setBackgroundImage:_defaultImage forBarMetrics:UIBarMetricsDefault];
}
一念一轮回 2024-09-22 00:51:12

您可以在应用程序中的任何位置使用此代码(调用 viewWillAppeare 中的方法:),通过调用该方法来更改导航栏图像。如果您在 didFinishLanch 中调用该方法,则意味着导航栏图像已设置为整个应用程序。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  [[UIDevice currentDevice] systemVersion];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9)
    {
        [self customizeAppearance];
    }

}

- (void)customizeAppearance
{
    UIImage *navbarimage = [[UIImage imageNamed:@"blckapplication_bar.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0,0,0)];
    [[UINavigationBar appearance] setBackgroundImage:navbarimage forBarMetrics:UIBarMetricsDefault];

    // Create resizable images    
    // Set the background image for *all* UINavigationBars
}

You can use this code any where in your application(Call the method in viewWillAppeare:) by calling the method to change the navigation bar image. If You call the method in didFinishLanch means the navigation bar image is set to the whole app.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  [[UIDevice currentDevice] systemVersion];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] > 4.9)
    {
        [self customizeAppearance];
    }

}

- (void)customizeAppearance
{
    UIImage *navbarimage = [[UIImage imageNamed:@"blckapplication_bar.png"]resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0,0,0)];
    [[UINavigationBar appearance] setBackgroundImage:navbarimage forBarMetrics:UIBarMetricsDefault];

    // Create resizable images    
    // Set the background image for *all* UINavigationBars
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文