设置导航栏后退按钮swift中整个应用程序的图像

时间:2017-11-13 11:27:42

标签: ios swift uinavigationbar

我在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)

来自here

但用户界面不正确:

enter image description here

我也试过下面的代码:

UINavigationBar.appearance().backIndicatorImage = backButtonImage
UINavigationBar.appearance().backIndicatorTransitionMaskImage = backButtonImage
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -80.0), for: .default)

看起来像这样:

enter image description here

但它在终端中给出了约束问题:

[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.

还有其他方法吗?或者我做错了什么?

2 个答案:

答案 0 :(得分:2)

参考@ Rajamohan的回答:

  

Change UINavigationBar back button text and font from AppDelegate using Swift

您可以使用以下代码执行此操作;

 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
    }

}

答案 1 :(得分:0)

有人可能会发布一个更好的答案,但我的解决方案是放弃导航栏及其外观这些废话,所以我所做的是为我在应用程序中使用的所有视图控制器创建一个基本视图控制器。 (无论如何我这样做,所以我能够控制状态栏样式,翻译......)。

所以我的基本视图控制器看起来有点像:

datagrid

因此,所有视图控制器都会完全覆盖后退按钮以显示所需的图像。然后,这会产生一个问题,即滑动中断以返回到上一个视图控制器,以便委托修复它。

我对我的解决方案不是很满意,但它似乎是唯一一个在iOS版本中始终如一的人。

请注意,如果您使用像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 } } UITableViewController这样的愚蠢控制器,您还需要对它们进行子类化并在其中添加相同的代码。

对于导航视图控制器,我建议你对它进行子类化和/或甚至是UITabBarViewController的子类化。通过这种方式,您可以完全控制正在发生的事情及其外观。