使用 UIBarButtonItemAppearance 同时具有系统和自定义后退按钮图像
Using UIBarButtonItemAppearance have both the system and the custom back button images
我是第一次使用 UIBarButtonItemAppearance
,我对如何为后退按钮放置自定义图像感到困惑。
我是这样做的:
private func createBarButtonAppearence(_ color: UIColor, textColor: UIColor) -> UINavigationBarAppearance {
let appearance = UINavigationBarAppearance()
appearance.titleTextAttributes = [.foregroundColor: textColor]
appearance.largeTitleTextAttributes = [.foregroundColor: textColor]
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = color
let back = UIBarButtonItemAppearance()
back.normal.backgroundImage = UIImage(named: "white_back_arrow")
appearance.backButtonAppearance = back
return appearance
}
这成功地将 "white_back_arrow"
图像作为后退按钮,但它也保留了原始的 iOS 后退按钮图像,如图所示:
如何避免这种行为?
您正在设置按钮的背景图片。这不会改变 按钮的 图像。
这样试试:
private func createBarButtonAppearence(_ color: UIColor, textColor: UIColor) -> UINavigationBarAppearance {
let appearance = UINavigationBarAppearance()
appearance.titleTextAttributes = [.foregroundColor: textColor]
appearance.largeTitleTextAttributes = [.foregroundColor: textColor]
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = color
// do this
if let img = UIImage(systemName: "white_back_arrow") {
appearance.setBackIndicatorImage(img, transitionMaskImage: img)
}
// don't do this:
//let back = UIBarButtonItemAppearance()
//back.normal.backgroundImage = UIImage(named: "white_back_arrow")
//appearance.backButtonAppearance = back
return appearance
}
我是第一次使用 UIBarButtonItemAppearance
,我对如何为后退按钮放置自定义图像感到困惑。
我是这样做的:
private func createBarButtonAppearence(_ color: UIColor, textColor: UIColor) -> UINavigationBarAppearance {
let appearance = UINavigationBarAppearance()
appearance.titleTextAttributes = [.foregroundColor: textColor]
appearance.largeTitleTextAttributes = [.foregroundColor: textColor]
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = color
let back = UIBarButtonItemAppearance()
back.normal.backgroundImage = UIImage(named: "white_back_arrow")
appearance.backButtonAppearance = back
return appearance
}
这成功地将 "white_back_arrow"
图像作为后退按钮,但它也保留了原始的 iOS 后退按钮图像,如图所示:
如何避免这种行为?
您正在设置按钮的背景图片。这不会改变 按钮的 图像。
这样试试:
private func createBarButtonAppearence(_ color: UIColor, textColor: UIColor) -> UINavigationBarAppearance {
let appearance = UINavigationBarAppearance()
appearance.titleTextAttributes = [.foregroundColor: textColor]
appearance.largeTitleTextAttributes = [.foregroundColor: textColor]
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = color
// do this
if let img = UIImage(systemName: "white_back_arrow") {
appearance.setBackIndicatorImage(img, transitionMaskImage: img)
}
// don't do this:
//let back = UIBarButtonItemAppearance()
//back.normal.backgroundImage = UIImage(named: "white_back_arrow")
//appearance.backButtonAppearance = back
return appearance
}