使 UINavigationBar 透明
如何使 UINavigationBar 透明?虽然我希望它的酒吧项目保持可见。
How do you make a UINavigationBar transparent? Though I want its bar items to remain visible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
如果有人想知道如何在 iOS 7+ 中实现此目的,这里有一个解决方案(也兼容 iOS 6)
In Objective-C
In swift 3 (iOS 10)
In swift 2
Discussion
将
translucent
设置为由于
UINavigationBar
文档。我将在这里报告相关片段:If anybody is wondering how to achieve this in iOS 7+, here's a solution (iOS 6 compatible too)
In Objective-C
In swift 3 (iOS 10)
In swift 2
Discussion
Setting
translucent
toYES
on the navigation bar does the trick, due to a behavior discussed in theUINavigationBar
documentation. I'll report here the relevant fragment:在 iOS5 中,您可以这样做以使导航栏透明:
In iOS5 you can do this to make the navigation bar transparent:
从 iOS7 开始:
From IOS7 :
如果您使用最新的 beta iOS 13.4 和 Xcode 11.4 进行构建,则接受的答案将不再有效。
我找到了另一种方法,也许这只是测试版软件中的一个错误,但我把它写在那里,以防万一
(swift 5)
If you build with the latest beta iOS 13.4 and Xcode 11.4, the accepted answer won't work anymore.
I've found another way, maybe it's just a bug in the beta software, but I'm writing it down there, just in case
(swift 5)
对于任何想要在 Swift 2.x
或 Swift 3.x 中执行此操作的人:
For anyone who wants to do this in Swift 2.x:
or Swift 3.x:
这似乎有效:
This seems to work:
完成上面其他人所说的操作后,即:
...我的导航栏仍然是白色的。所以我添加了这一行:
...等等瞧!这似乎成功了。
After doing what everyone else said above, i.e.:
... my navigation bar was still white. So I added this line:
... et voila! That seemed to do the trick.
解决方案 - Swift 5 - iOS 13+
根据 文档,在 UIViewController 中子类:
需要明确的是,这使得
UINavigationBar
完全透明。栏按钮项目仍然可见并且可以正常工作。什么不起作用
这让我意识到我实际上并不知道 透明 和半透明 RIP。
参考文献
https://developer.apple.com/documentation/uikit/uinavigationcontroller/customizing_your_app_s_navigation_bar
https://www.lexico.com/en/definition/transparent
< a href="https://www.lexico.com/en/definition/translucent" rel="nofollow noreferrer">https://www.lexico.com/en/definition/translucent
更新 08/10 /2021
按照我提供的方式设置外观后更改
navigationItem
栏按钮将重置外观,您必须再次执行此操作。Solution - Swift 5 - iOS 13+
According to the documentation, in your UIViewController subclass:
Just to be clear, this makes the
UINavigationBar
completely transparent. The bar button items are still visible and work properly.What didn't work
This made me realize I didn't actually know the difference between transparent and translucent RIP.
References
https://developer.apple.com/documentation/uikit/uinavigationcontroller/customizing_your_app_s_navigation_bar
https://www.lexico.com/en/definition/transparent
https://www.lexico.com/en/definition/translucent
Update 08/10/2021
Changing the
navigationItem
bar buttons after setting the appearance in the way I provided will reset the appearance and you'll have to do it again.我知道这个话题已经很老了,但是如果人们想知道它是如何在不重载drawRect方法的情况下完成的。
这就是您所需要的:
I know this topic is old, but if people want to know how its done without overloading the drawRect method.
This is what you need:
下面的代码扩展了为此线程选择的最佳答案,以消除底部边框并设置文本颜色:
此代码的最后两行代码设置透明度。我从这个线程借用了该代码,它运行得非常好!
“clipsToBounds”属性是我发现的代码,它摆脱了带有或不设置透明度的底部边框线(因此,如果您决定使用纯白色/黑色/等背景,仍然不会有边框行)。
“tintColor”行(第二个编码行)将我的后退按钮设置为浅灰色
我将 barTintColor 保留为一个备份。我不知道为什么透明度不起作用,但如果不起作用,我希望我的背景像以前一样是白色的
The below code expands upon the top answer chosen for this thread, to get rid of the bottom border and set text color:
The last two coded lines of this code set transparency. I borrowed that code from this thread and it worked perfectly!
The "clipsToBounds" property was code I found which got rid of the bottom border line with OR without transparency set (so if you decide to go with a solid white/black/etc. background instead, there will still be no border line).
The "tintColor" line (2nd coded line) set my back button to a light grey
I kept barTintColor as a backup. I don't know why transparency would not work, but if it doesn't, I want my bg white as I used to have it
对于斯威夫特 3.0:
for Swift 3.0:
C#/Xamarin 解决方案
C# / Xamarin Solution
尝试下面的代码:
Try the following piece of code:
对我有用的另一种方法是子类化 UINavigationBar 并将 drawRect 方法留空!
Another Way That worked for me is to Subclass UINavigationBar And leave the drawRect Method empty !!
在 Swift 4.2 中
(在 viewWillAppear 中),然后在 viewWillDisappear 中,要撤消它,请将
In Swift 4.2
(in viewWillAppear), and then in viewWillDisappear, to undo it, put
这适用于 Swift 5。
This worked with Swift 5.
您的意思是完全透明,还是使用“照片”应用程序中看到的半透明黑色样式?后者可以通过将其
barStyle
属性设置为UIBarStyleBlackTranslucent
来实现。前者……我不确定。如果您希望其上的项目仍然可见,您可能需要在栏的视图层次结构中进行一些挖掘,并删除包含其背景的视图。Do you mean entirely transparent, or using the translucent-black style seen in the Photos app? The latter you can accomplish by setting its
barStyle
property toUIBarStyleBlackTranslucent
. The former... I'm not sure about. If you want the items on it to still be visible, you might have to do some digging around in the bar's view hierarchy and remove the view containing its background.这适用于 Swift 2.0。
This works for Swift 2.0.
检查 RRViewControllerExtension,它专门用于 UINavigation 栏外观管理。
在您的项目中使用 RRViewControllerExtension,您只需要
在视图控制器中重写即可。
Check RRViewControllerExtension, which is dedicated on UINavigation bar appearance management.
with RRViewControllerExtension in your project, you just need to override
in you viewcontroller.