11导航栏中的UIBarButtonItem负隔离片

11导航栏中的UIBarButtonItem负隔离片

本文介绍了iOS 11导航栏中的UIBarButtonItem负隔离片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

在iOS 10及更低版本中,有一种方法可以向导航栏中的按钮数组添加一个负间隔符,如下所示:

In iOS 10 and below, there was a way to add a negative spacer to the buttons array in the navigation bar, like so:

UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = -8;
self.navigationItem.leftBarButtonItems = @[negativeSpacer, [self backButtonItem]];

这在iOS 11上不再起作用(间隔变为正数,而不是负数).我已经检查了条形按钮项的视图层次结构,现在将其嵌入到_UIButtonBarStackView中.如何在iOS 11上调整长条按钮的位置?

This no longer works on iOS 11 (the spacer becomes positive, instead of negative). I have inspected the view hierarchy of the bar button item, and it is now embedded into _UIButtonBarStackView. How to adjust the position of the bar button on iOS 11?

推荐答案

从iOS 13开始,这可能不再起作用.您可能会收到错误消息:

This may no longer work as of iOS 13. You may get the error:

旧答案:

我在Apple开发者论坛上发现了一个有点骇人听闻的解决方案: https://forums.developer.apple.com/thread/80075

I found a somewhat hacky solution on the Apple developer forums:https://forums.developer.apple.com/thread/80075

问题似乎出在iOS 11如何处理UIBarButtonItem .fixedSpace按钮以及iOS 11中UINavigationBar的布局方式.导航栏现在使用自动布局和布局边距来布局按钮.该帖子(底部)中提出的解决方案是将所有布局边距设置为所需的某个值.

It looks like the problem comes from how iOS 11 handles the UIBarButtonItem .fixedSpace buttons and how a UINavigationBar is laid out in iOS 11. The navigation bars now use autolayout and the layout margins to layout the buttons. The solution presented in that post (at the bottom) was to set all the layout margins to some value you want.

class InsetButtonsNavigationBar: UINavigationBar {

    override func layoutSubviews() {
        super.layoutSubviews()

        for view in subviews {
            // Setting the layout margins to 0 lines the bar buttons items up at
            // the edges of the screen. You can set this to any number to change
            // the spacing.
            view.layoutMargins = .zero
        }
    }

}

要使用具有自定义按钮间距的新导航栏,您将需要使用以下代码更新创建导航控制器的位置:

To use this new nav bar with custom button spacing, you will need to update where you create any navigation controllers with the following code:

let navController = UINavigationController(navigationBarClass: InsetButtonsNavigationBar.self,
                                                 toolbarClass: UIToolbar.self)
navController.viewControllers = [yourRootViewController]

这篇关于iOS 11导航栏中的UIBarButtonItem负隔离片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 14:22