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
}
}
}
网友评论