简体   繁体   English

使 UINavigationBar 透明

[英]Make UINavigationBar transparent

How do you make a UINavigationBar transparent ?你如何使UINavigationBar 透明 Though I want its bar items to remain visible.虽然我希望它的酒吧项目保持可见。

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.由于UINavigationBar文档中讨论的行为,在导航栏上将translucent设置为YES可以解决问题。 I'll report here the relevant fragment:我将在这里报告相关片段:

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 的系统不透明度。

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];

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];

If you build with the latest beta iOS 13.4 and Xcode 11.4, the accepted answer won't work anymore.如果您使用最新的测试版 iOS 13.4 和 Xcode 11.4 构建,则接受的答案将不再有效。 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) (迅速 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
        }
    }
}

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

This seems to work:这似乎有效:

@implementation UINavigationBar (custom)
- (void)drawRect:(CGRect)rect {}
@end

navigationController.navigationBar.backgroundColor = [UIColor clearColor];

After doing what everyone else said above, ie:在做了上面其他人所说的之后,即:

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.这似乎奏效了。

The below code expands upon the top answer chosen for this thread, to get rid of the bottom border and set text color:下面的代码扩展了为此线程选择的最佳答案,以摆脱底部边框并设置文本颜色:

  1. The last two coded lines of this code set transparency.此代码的最后两行代码设置透明度。 I borrowed that code from this thread and it worked perfectly!我从该线程中借用了该代码,并且效果很好!

  2. 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). “clipsToBounds”属性是我发现的代码,它在没有设置透明度的情况下去掉了底部边框线(因此,如果您决定使用纯白色/黑色/等背景代替,仍然没有边框线)。

  3. The "tintColor" line (2nd coded line) set my back button to a light grey “tintColor”行(第二行编码)将我的后退按钮设置为浅灰色

  4. I kept barTintColor as a backup.我保留了 barTintColor 作为备份。 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我不知道为什么透明度不起作用,但如果不起作用,我想要我的 bg white 就像我曾经拥有的那样

    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()

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];

Solution - Swift 5 - iOS 13+解决方案 - Swift 5 - iOS 13+

According to the documentation , in your UIViewController subclass:根据文档,在您的 UIViewController 子类中:

override func viewDidLoad()
{
    super.viewDidLoad()
    
    let appearance = UINavigationBarAppearance()
    appearance.configureWithTransparentBackground()
    //appearance.backgroundColor = UIColor.clear
    
    navigationItem.compactAppearance = appearance
    navigationItem.scrollEdgeAppearance = appearance
    navigationItem.standardAppearance = appearance
    
    //...
}

Just to be clear, this makes the UINavigationBar completely transparent .为了清楚起见,这使得UINavigationBar完全透明 The bar button items are still visible and work properly.条形按钮项仍然可见并且可以正常工作。

What didn't work什么没用

override func viewDidLoad()
{
    super.viewDidLoad()
    
    navigationController?.navigationBar.isTranslucent = true
    navigationController?.navigationBar.isOpaque = false

    //...
}

This made me realize I didn't actually know the difference between transparent and translucent RIP.这让我意识到我实际上并不知道透明半透明RIP 之间的区别。

References参考

https://developer.apple.com/documentation/uikit/uinavigationcontroller/customizing_your_app_s_navigation_bar 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/transparent

https://www.lexico.com/en/definition/translucent https://www.lexico.com/en/definition/translucent

Update 08/10/2021 2021 年 8 月 10 日更新

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.以我提供的方式设置外观后更改navigationItem栏按钮将重置外观,您必须再次执行此操作。

C# / Xamarin Solution C# / Xamarin 解决方案

NavigationController.NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
NavigationController.NavigationBar.ShadowImage = new UIImage();
NavigationController.NavigationBar.Translucent = true;

for Swift 3.0:对于 Swift 3.0:

override func viewDidLoad() {
    super.viewDidLoad()

    navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    navigationController?.navigationBar.shadowImage = UIImage()
    navigationController?.navigationBar.isTranslucent = true
}

试试下面的一段代码:

self.navigationController.navigationBar.translucent = YES;

Another Way That worked for me is to Subclass UINavigationBar And leave the drawRect Method empty !!对我有用的另一种方法是子类 UINavigationBar 并将 drawRect 方法留空!

@IBDesignable class MONavigationBar: UINavigationBar {


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
    // Drawing code
}}

In Swift 4.2在斯威夫特 4.2

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

(in viewWillAppear), and then in viewWillDisappear, to undo it, put (在 viewWillAppear 中),然后在 viewWillDisappear 中,要撤消它,放

self.navigationController?.navigationBar.shadowImage = nil
self.navigationController?.navigationBar.isTranslucent = false

This worked with Swift 5.这适用于 Swift 5。

// Clear the background image.
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)

// Clear the shadow image.
navigationController?.navigationBar.shadowImage = UIImage()

// Ensure the navigation bar is translucent.
navigationController?.navigationBar.isTranslucent = true

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 to UIBarStyleBlackTranslucent .后者可以通过将其barStyle属性设置为UIBarStyleBlackTranslucent来完成。 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.如果您希望其上的项目仍然可见,您可能需要在栏的视图层次结构中进行一些挖掘并删除包含其背景的视图。

This works for Swift 2.0.这适用于 Swift 2.0。

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

Check RRViewControllerExtension , which is dedicated on UINavigation bar appearance management.检查RRViewControllerExtension ,它专门用于 UINavigation 栏外观管理。

with RRViewControllerExtension in your project, you just need to override在您的项目中使用 RRViewControllerExtension,您只需要覆盖

-(BOOL)prefersNavigationBarTransparent;

in you viewcontroller.在您的视图控制器中。

导航栏透明

Swift 5<\/strong> :: Calling below in AppDelegate's didFinishLaunchingWithOptions function does the trick (This will be applied to your all navigationBars though, don't forget to switch your view controllers) Swift 5<\/strong> :: 在 AppDelegate 的 didFinishLaunchingWithOptions 函数中调用下面的技巧(这将应用于您的所有导航栏,但不要忘记切换您的视图控制器)

let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithTransparentBackground()
            
navigationController?.navigationBar.standardAppearance = navBarAppearance
navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
extension UINavigationBar {
var isTransperent: Bool {
        get {
            return false // Just to satisfy property
        }
        set {
            if newValue {
                self.shadowImage = UIImage()
                self.isTranslucent = true
                self.setBackgroundImage(UIImage(), for: .default)
            } else {
                self.shadowImage = UIImage()
                self.isTranslucent = false
                self.setBackgroundImage(nil, for: .default)
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM