【问题标题】:Back button not aligned properly in iOS 11iOS 11 中的后退按钮未正确对齐
【发布时间】:2017-10-13 05:30:23
【问题描述】:

我们开始使用 Xcode 8.3 开发我们的项目,在 iOS 10 之前一切都很好,但是当我们在 iOS 11 中运行相同的应用程序时,后退按钮未按预期对齐,如下图所示

但在 iOS 10 中,它可以正确对齐

这就是我们得到的布局约束错误

(
"<NSLayoutConstraint:0x600000288200 _UIModernBarButton:0x7f7ef5c87f10.bottom == UILayoutGuide:0x6000005a0380'UIViewLayoutMarginsGuide'.bottom + 64.5   (active)>",
"<NSLayoutConstraint:0x600000287f30 V:[_UIModernBarButton:0x7f7ef5c87f10]-(>=0)-|   (active, names: '|':_UIButtonBarButton:0x7f7ef5c86e60 )>",
"<NSLayoutConstraint:0x600000282030 'UIView-bottomMargin-guide-constraint' V:[UILayoutGuide:0x6000005a0380'UIViewLayoutMarginsGuide']-(16)-|   (active, names: '|':_UIButtonBarButton:0x7f7ef5c86e60 )>"

)

【问题讨论】:

    标签: uibarbuttonitem ios11 navigationbar pushviewcontroller


    【解决方案1】:

    对我来说这是因为我使用了一些类似下面的技巧来隐藏标题

    UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -1000), for: .default)
    

    我们不应该这样做,它在 iOS 11 上不起作用,并且会像您发布的那样触发很多自动布局问题。所以检查你是否在返回按钮上有appearance

    如果你真的想隐藏后退按钮上的文字,你应该这样做https://stackoverflow.com/a/46889050/1418457

    【讨论】:

      【解决方案2】:
      UINavigationBar.appearance().backIndicatorImage = image.withRenderingMode(.alwaysOriginal)
      UINavigationBar.appearance().backIndicatorTransitionMaskImage = image.withRenderingMode(.alwaysOriginal)
      
          if #available(iOS 11, *) {
              UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .normal)
              UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .highlighted)
          } else {
              UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffset(horizontal: -60, vertical: -60), for: .default)
          }
      

      【讨论】:

      • 在为 iOS 11 之前的设备删除 else 条件时,此代码仍然有效。那么我可以知道什么对 else 条件很重要吗?
      • 对于低于 ios 11
      • 是的,我从上面的代码中理解了这一点,但是当我删除 else 代码时,iOS 10 设备仍然看起来是正确的 UI。
      • 太好了,我会检查所有 ios(大于 ios7)。如果它适用于一个代码,那么我肯定会删除另一个代码
      猜你喜欢
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 2013-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多