ios 使 UINavigationBar 透明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2315862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make UINavigationBar transparent
提问by quano
How do you make a UINavigationBar transparent? Though I want its bar items to remain visible.
你如何使UINavigationBar 透明?虽然我想它的物品栏仍然可见。
回答by Gabriele Petronella
If anybody is wondering how to achieve this in iOS 7+, here's a solution (iOS 6 compatible too)
如果有人想知道如何在 iOS 7+ 中实现这一点,这里有一个解决方案(iOS 6 也兼容)
In Objective-C
在 Objective-C 中
[self.navigationBar setBackgroundImage:[UIImage new]
forBarMetrics:UIBarMetricsDefault];
self.navigationBar.shadowImage = [UIImage new];
self.navigationBar.translucent = YES;
In swift 3 (iOS 10)
在 swift 3 (iOS 10)
self.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.isTranslucent = true
In swift 2
在迅速 2
self.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.translucent = true
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:
由于文档中讨论的行为,在导航栏上设置translucent
为可以YES
解决问题UINavigationBar
。我将在这里报告相关片段:
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.
如果
YES
在带有不透明自定义背景图像的导航栏上将此属性设置为,导航栏将对图像应用小于 1.0 的系统不透明度。
回答by Alex Stanciu
In iOS5 you can do this to make the navigation bar transparent:
在 iOS5 中,您可以这样做以使导航栏透明:
nav.navigationBar.translucent = YES; // Setting this slides the view up, underneath the nav bar (otherwise it'll appear black)
const float colorMask[6] = {222, 255, 222, 255, 222, 255};
UIImage *img = [[UIImage alloc] init];
UIImage *maskedImage = [UIImage imageWithCGImage: CGImageCreateWithMaskingColors(img.CGImage, colorMask)];
[nav.navigationBar setBackgroundImage:maskedImage forBarMetrics:UIBarMetricsDefault];
[img release];
回答by Damien Romito
From IOS7 :
从 IOS7 :
self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.view.backgroundColor = [UIColor clearColor];
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
回答by fulvio
For anyone who wants to do this in Swift 2.x:
对于想在 Swift 2.x 中执行此操作的任何人:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.translucent = true
or Swift 3.x:
或 Swift 3.x:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
回答by quano
This seems to work:
这似乎有效:
@implementation UINavigationBar (custom)
- (void)drawRect:(CGRect)rect {}
@end
navigationController.navigationBar.backgroundColor = [UIColor clearColor];
回答by Clay Ellis
After doing what everyone else said above, i.e.:
在做了上面其他人所说的之后,即:
navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController!.navigationBar.isTranslucent = true
... my navigation bar was still white. So I added this line:
...我的导航栏还是白色的。所以我添加了这一行:
navigationController?.navigationBar.backgroundColor = .clear
... et voila! That seemed to do the trick.
……等等!这似乎奏效了。
回答by Picci
If you build with the lastest 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
如果您使用最新的测试版 iOS 13.4 和 XCode 11.4 进行构建,则已接受的答案将不再适用。我找到了另一种方法,也许它只是测试版软件中的一个错误,但我把它写在那里,以防万一
(swift 5)
(快速 5)
import UIKit
class TransparentNavBar :UINavigationBar {
override func awakeFromNib() {
super.awakeFromNib()
self.setBackgroundImage(UIImage(), for: .default)
self.shadowImage = UIImage()
self.isTranslucent = true
self.backgroundColor = .clear
if #available(iOS 13.0, *) {
self.standardAppearance.backgroundColor = .clear
self.standardAppearance.backgroundEffect = .none
self.standardAppearance.shadowColor = .clear
}
}
}
回答by Dave G
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
let navigationBarAppearace = UINavigationBar.appearance() navigationBarAppearace.tintColor = UIColor.lightGray navigationBarAppearace.barTintColor = UIColor.white navigationBarAppearace.clipsToBounds = true navigationBarAppearace.isTranslucent = true navigationBarAppearace.setBackgroundImage(UIImage(), for: .default) navigationBarAppearace.shadowImage = UIImage()
此代码的最后两行编码设置透明度。我从这个线程借用了那个代码,它运行得很好!
“clipsToBounds”属性是我发现的代码,它使用 OR 去掉了没有设置透明度的底部边界线(因此,如果您决定使用纯白色/黑色/等背景代替,仍然不会有边界线)。
“tintColor”行(第二个编码行)将我的后退按钮设置为浅灰色
我保留了 barTintColor 作为备份。我不知道为什么透明度不起作用,但如果它不起作用,我希望我的 bg 像以前一样是白色的
let navigationBarAppearace = UINavigationBar.appearance() navigationBarAppearace.tintColor = UIColor.lightGray navigationBarAppearace.barTintColor = UIColor.white navigationBarAppearace.clipsToBounds = true navigationBarAppearace.isTranslucent = true navigationBarAppearace.setBackgroundImage(UIImage(), for: .default) navigationBarAppearace.shadowImage = UIImage()
回答by Sander
I know this topic is old, but if people want to know how its done without overloading the drawRect method.
我知道这个话题很老,但是如果人们想知道它是如何在不重载 drawRect 方法的情况下完成的。
This is what you need:
这是你需要的:
self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.opaque = YES;
self.navigationController.navigationBar.tintColor = [UIColor clearColor];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
回答by Mark Moeykens
C# / Xamarin Solution
C#/Xamarin 解决方案
NavigationController.NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
NavigationController.NavigationBar.ShadowImage = new UIImage();
NavigationController.NavigationBar.Translucent = true;