美文网首页
iOS navigationBar导航栏(swift版本),如何

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

作者: 可乐小子 | 来源:发表于2024-02-21 14:49 被阅读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
    }
}

}

相关文章

网友评论

      本文标题:iOS navigationBar导航栏(swift版本),如何

      本文链接:https://www.haomeiwen.com/subject/qzoiadtx.html