iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – UINavigationBar UIToolBar UIAppearance以及应用程序某些部分的半透明barStyle?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用以下代码为我的UINavigationBar和UIToolbar设置自定义图像:

[[UINavigationBar appearance] setBACkgroundImage:[UIImage imagenamed:@"navigationBar"] forBarMetrics:UIBarMetricsDefault];
[[UIToolbar appearance] setBACkgroundImage:[UIImage imagenamed:@"toolbar"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

我正在使用PhotoViewer并将其视图控制器推入视图中.它应该有一个半透明的导航栏和工具栏,但它使用我提供的图形半透明.

问题是,稍后当我推入另一个视图控制器(从PhotoViewer弹回超级)后,它的工具栏也是半透明的,这意味着内容就在它后面.

没有运气,我尝试了以下内容

[[UINavigationBar appearanceWhenContainedIn:[EGOPhotoViewController class],nil] setBarStyle:UIBarStyleBlackTranslucent];
[[UINavigationBar appearanceWhenContainedIn:[EGOPhotoViewController class],nil] setBACkgroundImage:nil forBarMetrics:UIBarMetricsDefault];
[[UIToolbar appearanceWhenContainedIn:[EGOPhotoViewController class],nil] setBarStyle:UIBarStyleBlackTranslucent];
[[UIToolbar appearanceWhenContainedIn:[EGOPhotoViewController class],nil] setBACkgroundImage:nil forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];

任何想法如何我可以为PhotoViewer实现黑色半透明barStyle并保留其他所有我的自定义图形?

更新:为了获得一些帮助,我将一个示例项目与导航栏的自定义图形放在一起,然后在使用外观代理时尝试显示带有半透明导航栏的推送视图控制器,但没有成功:EXAMPLE PROJECT

解决方法

我已下载您的示例项目并设法修复它.我会解释问题所在.

首先,UINavigationBar包含在UINavigationController中.因此,RootViewController和TranslucentViewController使用相同的UINavigationBar实例.也许这会引起混乱.此外,这可能就是为什么外观以下内容无法按预期工作.

要在整个应用程序中设置导航栏的背景图像,您应该使用外观.要在导航控制器中设置单个导航栏的背景图像,请使用UINavigationBar的-setBACkgroundImagE:forBarMetrics :.

代码:in -TranslucentViewController viewWillAppear:设置背景图像和条形样式.在RootViewController中,再次设置背景图像和条形样式.根据我的经验,最好在-viewWillAppear中更改导航栏:而不是-in viewWillDisappear :(或者您需要跟踪要将其更改回来的内容.)

在RootViewController中

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self.navigationController.navigationBar setBarStyle:UIBarStyleDefault];
    [self.navigationController.navigationBar setBACkgroundImage:[UIImage imagenamed:@"orangeNavigationBar.png"] forBarMetrics:UIBarMetricsDefault];
}

在TranslucentViewController中

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

    [self.navigationController.navigationBar setBACkgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];
}

哦,并且:只在这些地方改变它.不是在推动视图控制器或任何东西时.

大佬总结

以上是大佬教程为你收集整理的ios – UINavigationBar UIToolBar UIAppearance以及应用程序某些部分的半透明barStyle?全部内容,希望文章能够帮你解决ios – UINavigationBar UIToolBar UIAppearance以及应用程序某些部分的半透明barStyle?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。