导航栏的后退按钮重叠,swift 4
Back Buttons from navigation bar are overlapping , swift 4
我试图根据 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) 自动出现。
所以我需要删除蓝色的。
你试过这样替换吗:
UINavigationBar.appearance().backIndicatorImage = UIImage(named: "back_arrow")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "back_arrow")
我已经在 AppDelegate
中的 applicationDidFinishLaunching
方法上尝试过这个
另一种设置透明色调的替代解决方案。请参阅下面修改后的代码。
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
}
有两件事需要完成你想要达到的目标:
- 将后退按钮的默认图像更改为您提供的图像
- 从后退按钮项目中删除标题
要更改后退按钮的图像:
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。该演示也有一些其他实现。但是你会得到你需要的和我上面说的。
我试图根据 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) 自动出现。
所以我需要删除蓝色的。
你试过这样替换吗:
UINavigationBar.appearance().backIndicatorImage = UIImage(named: "back_arrow")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "back_arrow")
我已经在 AppDelegate
applicationDidFinishLaunching
方法上尝试过这个
另一种设置透明色调的替代解决方案。请参阅下面修改后的代码。
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
}
有两件事需要完成你想要达到的目标:
- 将后退按钮的默认图像更改为您提供的图像
- 从后退按钮项目中删除标题
要更改后退按钮的图像:
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。该演示也有一些其他实现。但是你会得到你需要的和我上面说的。