简体   繁体   English

Swift UINavigation 底线和阴影去除,无需导航栏颜色更改

[英]Swift UINavigation Bottom Line and Shadow Remove without Navbar Color Change

My Scenario, I am trying to remove bottom line and shadow from UINavigationBar using iOS 13 - Swift 5 .我的场景,我正在尝试使用iOS 13 - Swift 5UINavigationBar删除底线和阴影。 Here, Before iOS 13 - Swift 5 , I used below code for removing bottom line and shadow without changing navigation bar color.在这里,在iOS 13 - Swift 5之前,我使用下面的代码在不改变导航栏颜色的情况下去除底线和阴影。 Now, It is not showing NavigationBar color (I already set Bar Color and Background Color) also disabled Transulent.现在,它没有显示NavigationBar颜色(我已经设置了 Bar Color 和 Background Color)也禁用了 Transulent。

Code:代码:

UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .default)

How to fix this?如何解决这个问题? Need to remove bottom line shadow and also want to give NavigationBar Colour.需要去除底线阴影,还想给 NavigationBar 颜色。

For me, it only worked after changing the following (>= iOS13)对我来说,它只有在更改以下内容后才有效(> = iOS13)

let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.shadowColor = .clear
navBarAppearance.shadowImage = UIImage()
navigationController?.navigationBar.standardAppearance = navBarAppearance
navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance

If you are using iOS 13, you can't remove the shadow by setting the shadowImage to UIImage() .如果您使用的是 iOS 13,则无法通过将shadowImage设置为UIImage()来移除阴影。 You'll have to set the shadowColor to nil or UIColor.clear .您必须将shadowColor设置为nilUIColor.clear

Try with this, I added in viewDidLoad.试试这个,我在 viewDidLoad 中添加了。 It works for me.这个对我有用。

self.navigationController?.navigationBar.shadowImage = UIImage()

Below Code working for me下面的代码为我工作

UINavigationBar.appearance().setBackgroundImage(UIImage(), for: UIBarMetrics.default)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().barTintColor = #colorLiteral
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]

This seems to be working for iOS13 and Swift这似乎适用于 iOS13 和 Swift

let barAppearance = UINavigationBarAppearance()
barAppearance.configureWithTransparentBackground()

XCODE 13 - iOS 15 XCODE 13 - iOS 15

For Xcode 13 and targeting iOS ver 13 and above you can use this code:对于 Xcode 13 并针对 iOS ver 13 及更高版本,您可以使用以下代码:

 if #available(iOS 13.0, *) {
                let appearance = UINavigationBarAppearance()
                appearance.configureWithOpaqueBackground()
                appearance.backgroundColor = .red // Your color
                appearance.shadowColor = .clear
                appearance.shadowImage = UIImage()
                navigationController?.navigationBar.standardAppearance = appearance;
                navigationController?.navigationBar.scrollEdgeAppearance = navigationController?.navigationBar.standardAppearance

            }

If you want to do with UINavigationBarAppearance you can do with this.如果您想使用 UINavigationBarAppearance,您可以使用它。

let appearance = UINavigationBarAppearance()
appearance.configureWithDefaultBackground()
appearance.shadowImage = UIImage()
appearance.shadowColor = UIColor.clear
appearance.backgroundImage = UIImage()
appearance.backgroundColor = UIColor.white
UINavigationBar.appearance().scrollEdgeAppearance = appearance

Starting from iOS 15 and in order to fix wrong navigation bar color glitch, you have to use relatively newest API of UINavigationBarAppearance and tiny but important moment - to refresh navigation bar if it's already on the screen.从 iOS 15 开始,为了修复错误的导航栏颜色故障,您必须使用 UINavigationBarAppearance 的相对最新的 API 和微小但重要的时刻 - 如果导航栏已经在屏幕上,则刷新导航栏。 Below is the full code:下面是完整的代码:

if #available(iOS 15.0, *) {
      let appearance = UINavigationBarAppearance()
      appearance.configureWithOpaqueBackground()
      appearance.backgroundColor = <#your_color#>
      appearance.shadowImage = UIImage()
      appearance.shadowColor = .clear
      UINavigationBar.appearance().standardAppearance = appearance
      UINavigationBar.appearance().compactAppearance = appearance
      UINavigationBar.appearance().scrollEdgeAppearance = appearance
      UINavigationBar.appearance().compactScrollEdgeAppearance = appearance

      // force refresh navigation bar background color
      isNavigationBarHidden = true
      isNavigationBarHidden = false
}

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

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