透明导航栏iOS 13

时间:2020-06-01 09:34:06

标签: ios swift uinavigationbar ios13 uinavigationbarappearance

无法创建透明的NavigationBar iOS 13。

我有自定义的UINavigationBar,在其中配置UINavigationBarAppearance

override init(frame: CGRect) {
    super.init(frame: frame)
    self.configure()
}

required init?(coder: NSCoder) {
    super.init(coder: coder)
    self.configure()
}

配置代码:

let appearance = UINavigationBarAppearance()
appearance.configureWithTransparentBackground()
appearance.shadowColor = .clear
appearance.backgroundColor = .clear

并将其设置为:

self.compactAppearance = appearance
self.standardAppearance = appearance
self.scrollEdgeAppearance = appearance

结果,我得到了这个:

enter image description here

预期结果:NavigationBar应该透明

3 个答案:

答案 0 :(得分:0)

尝试使用我的扩展程序:

extension UIViewController {
func configureNavigationBar(largeTitleColor: UIColor, backgoundColor: UIColor, tintColor: UIColor, title: String, preferredLargeTitle: Bool) {
    if #available(iOS 13.0, *) {
        let navBarAppearance = UINavigationBarAppearance()
        navBarAppearance.configureWithOpaqueBackground()
        navBarAppearance.largeTitleTextAttributes = [.foregroundColor: largeTitleColor]
        navBarAppearance.titleTextAttributes = [.foregroundColor: largeTitleColor]
        navBarAppearance.backgroundColor = backgoundColor

        navigationController?.navigationBar.standardAppearance = navBarAppearance
        navigationController?.navigationBar.compactAppearance = navBarAppearance
        navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance

        navigationController?.navigationBar.prefersLargeTitles = preferredLargeTitle
        navigationItem.largeTitleDisplayMode = .always
        navigationController?.navigationBar.isTranslucent = true
        navigationController?.navigationBar.tintColor = tintColor
        navigationController?.hideHairline() // remove navBar line
        navigationItem.title = title

    } else {
        // Fallback on earlier versions
        navigationController?.navigationBar.barTintColor = backgoundColor
        navigationController?.navigationBar.tintColor = tintColor
        navigationController?.navigationBar.isTranslucent = true
        navigationItem.title = title
    }
}
}

这是为了删除navBar行:

extension UINavigationController {
func hideHairline() {
    if let hairline = findHairlineImageViewUnder(navigationBar) {
        hairline.isHidden = true
    }
}
func restoreHairline() {
    if let hairline = findHairlineImageViewUnder(navigationBar) {
        hairline.isHidden = false
    }
}
func findHairlineImageViewUnder(_ view: UIView) -> UIImageView? {
    if view is UIImageView && view.bounds.size.height <= 1.0 {
        return view as? UIImageView
    }
    for subview in view.subviews {
        if let imageView = self.findHairlineImageViewUnder(subview) {
            return imageView
        }
    }
    return nil
}
}

现在像这样在viewDidLoad中调用它:

configureNavigationBar(largeTitleColor: .white, backgoundColor: .clear, tintColor: .white, title: "", preferredLargeTitle: true)
    let image = UIImage(systemName: "heart.fill")
    navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: self, action: #selector(yourFunction))

答案 1 :(得分:0)

修改外观代码:-

let appearance = UINavigationBarAppearance()
appearance.configureWithTransparentBackground()
appearance.shadowColor = .clear
appearance.backgroundColor = .clear
appearance.backgroundImage = nil
appearance.shadowImage = nil

答案 2 :(得分:0)

为我工作:

self.compactAppearance = appearance
self.standardAppearance = appearance
self.scrollEdgeAppearance = appearance
self.backgroundColor = appearance.backgroundColor