[英]Setting Navigation bar back button Image for entire application in swift
我在AddDelegate
设置了导航栏的后退按钮,但设置不正确。
我试过下面的代码:
let backButtonImage = backImage?.withRenderingMode(.alwaysOriginal).resizableImage(withCapInsets: UIEdgeInsetsMake((backImage?.size.height)!, (backImage?.size.width)!, 0, 0))
UIBarButtonItem.appearance().setBackButtonBackgroundImage(backButtonImage, for: .normal, barMetrics: .default)
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(-400, 0), for: .default)
从这里 。
但是用户界面不正确:
我也试过下面的代码:
UINavigationBar.appearance().backIndicatorImage = backButtonImage
UINavigationBar.appearance().backIndicatorTransitionMaskImage = backButtonImage
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -80.0), for: .default)
看起来像这样:
但这给终端带来了约束问题:
[LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x600000098510 UILayoutGuide:0x6040007aca20'BackButtonGuide(0x7f8143611390)'.height == 44 (active)>",
"<NSLayoutConstraint:0x60000029e320 _UIButtonBarButton:0x7f814349d110.top == UILayoutGuide:0x6040007aca20'BackButtonGuide(0x7f8143611390)'.top (active)>",
"<NSLayoutConstraint:0x60000029de20 _UIButtonBarButton:0x7f814349d110.bottom == UILayoutGuide:0x6040007aca20'BackButtonGuide(0x7f8143611390)'.bottom (active)>",
"<NSLayoutConstraint:0x60400049d790 _UIModernBarButton:0x7f81434a4800'Back'.lastBaseline == UILayoutGuide:0x6000001b51c0'UIViewLayoutMarginsGuide'.bottom - 80 (active)>",
"<NSLayoutConstraint:0x60400049d1a0 _UIModernBarButton:0x7f81434a4800'Back'.top >= _UIButtonBarButton:0x7f814349d110.top (active)>",
"<NSLayoutConstraint:0x600000481040 UIButtonLabel:0x7f81434a30d0'Back'.centerY == _UIModernBarButton:0x7f81434a4800'Back'.centerY + 1.5 (active)>",
"<NSLayoutConstraint:0x60000029ea00 'UIView-bottomMargin-guide-constraint' V:[UILayoutGuide:0x6000001b51c0'UIViewLayoutMarginsGuide']-(16)-| (active, names: '|':_UIButtonBarButton:0x7f814349d110 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600000481040 UIButtonLabel:0x7f81434a30d0'Back'.centerY == _UIModernBarButton:0x7f81434a4800'Back'.centerY + 1.5 (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
[LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x60000029f1d0 _UIModernBarButton:0x7f814360f630.bottom == UILayoutGuide:0x6040007ac940'UIViewLayoutMarginsGuide'.bottom + 84.5 (active)>",
"<NSLayoutConstraint:0x600000287bc0 V:[_UIModernBarButton:0x7f814360f630]-(>=0)-| (active, names: '|':_UIButtonBarButton:0x7f814360d8a0 )>",
"<NSLayoutConstraint:0x604000488930 'UIView-bottomMargin-guide-constraint' V:[UILayoutGuide:0x6040007ac940'UIViewLayoutMarginsGuide']-(16)-| (active, names: '|':_UIButtonBarButton:0x7f814360d8a0 )>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x60000029f1d0 _UIModernBarButton:0x7f814360f630.bottom == UILayoutGuide:0x6040007ac940'UIViewLayoutMarginsGuide'.bottom + 84.5 (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
还有其他方法吗,或者我做错了什么?
从@ Rajamohan-s的参考答案:
您可以使用以下代码来实现;
extension UINavigationItem{
override open func awakeFromNib() {
super.awakeFromNib()
let backItem = UIBarButtonItem()
backItem.title = "Hello"
if let font = UIFont(name: "Copperplate-Light", size: 32){
backItem.setTitleTextAttributes([NSFontAttributeName:font], for: .normal)
}else{
print("Font Not available")
}
/*Changing color*/
backItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.green], for: .normal)
self.backBarButtonItem = backItem
}
}
有人可能会发布更好的答案,但是我的解决方案是放弃导航栏及其外观的这些废话,所以我要做的是为我在应用程序内部使用的所有视图控制器创建一个基本视图控制器。 (无论如何我都这样做,所以我能够控制状态栏样式,翻译...)。
所以我的基本视图控制器看起来像:
class BaseViewController: UIViewController {
@IBInspectable var backButtonImage: UIImage? = /*Use your default here*/
override func viewDidLoad() {
super.viewDidLoad()
if let backButtonImage = backButtonImage {
navigationItem.leftBarButtonItem = UIBarButtonItem(image: backButtonImage, style: .plain, target: self, action: #selector(onBack))
navigationController?.interactivePopGestureRecognizer?.delegate = self
}
}
@objc private func onBack() {
navigationController?.popViewController(animated: true)
}
}
extension BaseViewController: UIGestureRecognizerDelegate {
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
return (navigationController?.viewControllers.count ?? 0) > 1
}
}
因此,后退按钮将完全覆盖所有视图控制器以显示所需的图像。 然后,这会产生一个问题,即滑动会中断以返回到先前的视图控制器,因此需要委托对其进行修复。
我对我的解决方案不是很满意,但它似乎是唯一在整个iOS版本中都能持续运行的解决方案。
请注意,如果使用愚蠢的控制器(如UITableViewController
或UITabBarViewController
,则还需要将其子类化,并在其中添加相同的代码。
至于导航视图控制器,我建议您也将其子类化,和/或甚至将UINavigationBar
子类化。 这样,您将完全控制正在发生的事情及其外观。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.