我试图根据this教程来自定义我的后退按钮。 在AppDelegate中,
let barButtonAppearence = UIBarButtonItem.appearance()
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
let backButton = UIImage(named: "back_arrow")
let backButtonImage = backButton?.stretchableImage(withLeftCapWidth: 0, topCapHeight: 10)
barButtonAppearence.setBackButtonBackgroundImage(backButtonImage, for: .normal, barMetrics: .default)
return true
}
然后它与现有的(由于segue(Show)而自动出现)冲突。
所以我需要删除蓝色的。
答案 0 :(得分:2)
要完成要达到的目标,需要完成两件事:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Remember, this will change every navigation bar's back button image in your app
UINavigationBar.appearance().backIndicatorImage = #imageLiteral(resourceName: "backButton")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = #imageLiteral(resourceName: "backButton")
return true
}
注意:如果您不希望所有的导航栏后退按钮都包含提供的图像,则可能需要继承UINavigationController
并更新其导航栏。
我们将通过扩展将方法添加到任何UIViewController
中。此处将使用扩展方法,以便任何UIViewController
都可以在需要时具有此行为。
extension UIViewController {
func removeNavigationBarBackButtonItemTitle() {
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItem.Style.plain, target: nil, action: nil)
}
}
现在,在 VC A -> VC B 的任何推送转换中,您都需要隐藏后退按钮的标题。但是您必须从 VC A 调用方法removeNavigationBarBackButtonItemTitle()
。只要记住这一点,就可以了。
class ViewControllerA: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
removeNavigationBarBackButtonItemTitle()
}
}
您将找到一个演示here。该演示还具有其他一些实现。但是您会得到您所需要的以及我上面所说的。
答案 1 :(得分:1)
您是否尝试过以这种方式替换它:
UINavigationBar.appearance().backIndicatorImage = UIImage(named: "back_arrow")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "back_arrow")
我已经在applicationDidFinishLaunching
的{{1}}方法上尝试过
答案 2 :(得分:1)
另一种可替代的解决方案,用于设置色调颜色清晰。请参阅您下面修改的代码。
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
let backButton = UIImage(named: "back_arrow")
let backButtonImage = backButton?.stretchableImage(withLeftCapWidth: 0, topCapHeight: 10)
barButtonAppearence.setBackButtonBackgroundImage(backButtonImage, for: .normal, barMetrics: .default)
barButtonAppearence.tintColor = UIColor.clear
return true
}