iOS 11 UINavigationBar 栏按钮项目对齐

2024-01-07

我正在将我的应用程序升级到iOS 11,我发现导航栏存在一些问题,我的部分问题已经在这里提出了问题,所以我不会在这个问题中提及它们。

所讨论的具体问题是导航栏的栏按钮项目的间距不同。我的主要左栏和右栏按钮项目现在更靠近屏幕的水平中心,我无法将它们移动到屏幕边缘附近。过去我用过一个自定义的UIButton子类化并使用自定义视图创建栏按钮项目。对齐解决方案是alignmentRectInsets and contentEdgeInsets,现在我无法使用这种方法产生预期的结果。

Edit:
我已使用 iOS 11 beta 2 重新测试,问题仍然存在。

Edit 2:我已经用 iOS beta 3 重新测试了,问题仍然存在。


现在,在 iOS 11 中,您可以管理 UINavigationBarContentView 来调整左右约束,并管理 UIStackView 来调整按钮(或其他元素)。

这是我的导航栏解决方案,左侧和右侧都有项目。如果您在一侧有多个按钮,它也可以修复。

- (void) updateNavigationBar {
    for(UIView *view in self.navigationBar.subviews) {
        if ([NSStringFromClass([view class]) containsString:@"ContentView"]) {

            // Adjust left and right constraints of the content view 
            for(NSLayoutConstraint *ctr in view.constraints) {
                if(ctr.firstAttribute == NSLayoutAttributeLeading || ctr.secondAttribute == NSLayoutAttributeLeading) {
                    ctr.constant = 0.f;
                } else if(ctr.firstAttribute == NSLayoutAttributeTrailing || ctr.secondAttribute == NSLayoutAttributeTrailing) {
                    ctr.constant = 0.f;
                }
            }

            // Adjust constraints between items in stack view
            for(UIView *subview in view.subviews) {
                if([subview isKindOfClass:[UIStackView class]]) {
                    for(NSLayoutConstraint *ctr in subview.constraints) {
                        if(ctr.firstAttribute == NSLayoutAttributeWidth || ctr.secondAttribute == NSLayoutAttributeWidth) {
                            ctr.constant = 0.f;
                        }
                    }
                }
            }
        }
    }
}


正如您所看到的,没有必要像其他人所做的那样添加更多约束。已经定义了约束,因此可以更改它们。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iOS 11 UINavigationBar 栏按钮项目对齐 的相关文章

随机推荐