gpt4 book ai didi

ios - iOS 11 导航栏中 UIBarButtonItem 的负间隔符

转载 作者:IT王子 更新时间:2023-10-29 07:57:48 25 4
gpt4 key购买 nike

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

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

这不再适用于 iOS 11(垫片变为正,而不是负)。我检查了条形按钮项的 View 层次结构,它现在嵌入到 _UIButtonBarStackView 中。如何在 iOS 11 上调整条形按钮的位置?

最佳答案

编辑:

从 iOS 13 开始,这可能不再有效。您可能会收到错误消息:

Client error attempting to change layout margins of a private view

旧答案:

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

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

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
}
}

}

要使用这个带有自定义按钮间距的新导航栏,您需要使用以下代码更新您创建任何导航 Controller 的位置:

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

关于ios - iOS 11 导航栏中 UIBarButtonItem 的负间隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45544961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com