如何在iOS7中绘制透明的UIToolbar或UINavigationBar [英] How to draw a transparent UIToolbar or UINavigationBar in iOS7

查看:98
本文介绍了如何在iOS7中绘制透明的UIToolbar或UINavigationBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个完全透明的 UIToolbar 和/或 UINavigationBar 。我已经尝试过为iOS 5之前和之后建议的各种咒语,但似乎没有任何工作。

I would like an entirely transparent UIToolbar and/or UINavigationBar. I have tried the various incantations suggested for pre- and post-iOS 5 but none seem to work any more.

在iOS 7中如何实现这一目标?

How might this be accomplished in iOS 7?

推荐答案

Swift 3(iOS 10)



透明 UIToolbar



Swift 3 (iOS 10)

Transparent UIToolbar

self.toolbar.setBackgroundImage(UIImage(),
                                forToolbarPosition: .any,
                                barMetrics: .default)
self.toolbar.setShadowImage(UIImage(), forToolbarPosition: .any)



透明 UINavigationBar



Transparent UINavigationBar

self.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.isTranslucent = true






Swift< 3



透明 UIToolbar




Swift < 3

Transparent UIToolbar

self.toolbar.setBackgroundImage(UIImage(),
                                forToolbarPosition: UIBarPosition.Any,
                                barMetrics: UIBarMetrics.Default)
self.toolbar.setShadowImage(UIImage(),
                            forToolbarPosition: UIBarPosition.Any)



透明 UINavigationBar



Transparent UINavigationBar

self.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.translucent = true






Objective-C



透明 UIToolbar




Objective-C

Transparent UIToolbar

[self.toolbar setBackgroundImage:[UIImage new]
              forToolbarPosition:UIBarPositionAny
                      barMetrics:UIBarMetricsDefault];
[self.toolbar setShadowImage:[UIImage new]
          forToolbarPosition:UIBarPositionAny];






透明 UINavigationBar




Transparent UINavigationBar

[self.navigationBar setBackgroundImage:[UIImage new]
                         forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];
self.navigationBar.translucent = YES;






讨论



在导航栏上设置半透明 YES 可以解决这个问题,原因在于 UINavigationBar 文档。我将在此处报告相关片段:


Discussion

Setting translucent to YES on the navigation bar does the trick, due to a behavior discussed in the UINavigationBar documentation. I'll report here the relevant fragment:


如果将此属性设置为 YES 在具有不透明自定义背景图像的导航栏上,导航栏将对图像应用小于1.0的系统不透明度。

If you set this property to YES on a navigation bar with an opaque custom background image, the navigation bar will apply a system opacity less than 1.0 to the image.






最终结果




Final result

这篇关于如何在iOS7中绘制透明的UIToolbar或UINavigationBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆