Прозрачная панель навигации iOS 13

Невозможно создать прозрачную панель навигации 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

И по результату у меня вот что:

Ожидаемый результат: NavigationBar должен быть прозрачным

3 ответа

Решение

Работает для меня:

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

Измените код внешнего вида на это:-

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

Попробуйте использовать мое расширение:

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))
Другие вопросы по тегам