自定义后退指示图像和iOS 11 [英] Custom back indicator image and iOS 11

查看:104
本文介绍了自定义后退指示图像和iOS 11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用中使用自定义后退按钮。这个自定义后退按钮全局设置如下:

I'm using a custom back button in my app. This custom back button is set globally like this:

    UINavigationBar.appearance().backIndicatorImage = UIImage(named: "Back").withRenderingMode(.alwaysOriginal)
    UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(asset: .back).withRenderingMode(.alwaysOriginal)

在iOS 11之前,这段代码可以解决问题,但现在在iOS 11中,按钮不再垂直居中,如下所示:

Before iOS 11 this code did the trick, but now in iOS 11 the button is not centered vertically anymore as you can see here:

我可以将后退按钮图像的高度更改为44,但这会在iOS< 11.我也可以使用两个不同的图像,但我正在寻找更清洁的东西,比如在后面按钮容器视图中垂直居中图像的方法。

I could change the height of the back button image to 44, but that would break it in iOS < 11. I could also use two different images, but I was looking for something cleaner, like a way to vertically center the image in the back button container view.

编辑:

事实证明,正如banxii1988所述,问题是由 setBackButtonTitlePositionAdjustment 引起的当值故意将标题移动到可见屏幕之外时。这是一个黑客,以避免删除每个视图控制器中的后退按钮标题。我决定删除这个黑客​​,我做了正确的事情:

Turns out that, as said by banxii1988, the problem is caused by setBackButtonTitlePositionAdjustment when the values deliberately move the title outside the visible screen. That was an hack to avoid removing the back button title in every view controller. I decided to remove this hack and I did the right thing which is:


  1. 将故事板中的后退按钮项设置为

  2. 在每个视图控制器中没有相关的故事板,我设置
    backBarButtonItem 以编程方式
    navigationItem.backBarButtonItem = UIBarButtonItem(title:,
    style:.plain,target:nil,action:nil)

  1. set the back button item in the storyboard to " ".
  2. in each view controller without an associated storyboard, I set the backBarButtonItem programmatically navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)

请注意,您在视图控制器中看到的后退按钮标题是在导航堆栈中的上一个中设置的。

Note that the back button title that you see in a view controller is set in the previous one in the navigation stack.

推荐答案

1)删除PositionAdjustment(如果有)。例如

1) remove PositionAdjustment if have any. such as

  bap.setBackButtonTitlePositionAdjustment(UIOffset(horizontal: 0, vertical: -64), for: .default)

2)检查导航堆栈中的前一个ViewController是否有标题

2) check if the previous ViewController in nav stack has a title

这篇关于自定义后退指示图像和iOS 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆