iOS navigationBar导航栏(swift版本),如何

2024-02-21  本文已影响0人  可乐小子

import UIKit

extension UINavigationBar {

func setBackgroundImage(_ img: UIImage?,_ color: UIColor) {
    if #available(iOS 13.0, *) {
        let appearance = UINavigationBarAppearance()
        appearance.configureWithTransparentBackground()
        appearance.backgroundImage = img
        appearance.backgroundColor = color
        appearance.shadowImage = img
        appearance.titleTextAttributes = standardAppearance.titleTextAttributes
        standardAppearance = appearance
        scrollEdgeAppearance = appearance
        compactAppearance = appearance
        if #available(iOS 15.0, *) {
            compactScrollEdgeAppearance = appearance
        } else {
            // Fallback on earlier versions
        }
    } else {

// setBackgroundImage(image, for: .default)
}
}

func setBackgroundImage(_ image: UIImage?) {
    if #available(iOS 15.0, *) {
        let appearance = UINavigationBarAppearance()
        appearance.configureWithTransparentBackground()
        appearance.backgroundImage = image
        appearance.shadowImage = standardAppearance.shadowImage
        appearance.titleTextAttributes = standardAppearance.titleTextAttributes
        standardAppearance = appearance
        scrollEdgeAppearance = appearance
        compactAppearance = appearance
        compactScrollEdgeAppearance = appearance
    } else {
        setBackgroundImage(image, for: .default)
    }
}


func setShadowImage(_ image: UIImage?) {
    if #available(iOS 15.0, *) {
        let appearance = UINavigationBarAppearance()
        appearance.configureWithTransparentBackground()
        appearance.backgroundImage = standardAppearance.backgroundImage
        appearance.shadowImage = image
        appearance.titleTextAttributes = standardAppearance.titleTextAttributes
        standardAppearance = appearance
        scrollEdgeAppearance = appearance
        compactAppearance = appearance
        compactScrollEdgeAppearance = appearance
    } else {
        shadowImage = image
    }
}

func setTitleTextAttributes(_ attributes: [NSAttributedString.Key: Any]) {
    if #available(iOS 15.0, *) {
        let appearance = UINavigationBarAppearance()
        appearance.configureWithTransparentBackground()
        appearance.backgroundImage = standardAppearance.backgroundImage
        appearance.shadowImage = standardAppearance.shadowImage
        appearance.titleTextAttributes = attributes
        standardAppearance = appearance
        scrollEdgeAppearance = appearance
        compactAppearance = appearance
        compactScrollEdgeAppearance = appearance
    } else {
        titleTextAttributes = attributes
    }
}

}

上一篇 下一篇

猜你喜欢

热点阅读