热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

iOS11UINavigationBar栏按钮项对齐

如何解决《iOS11UINavigationBar栏按钮项对齐》经验,为你挑选了3个好方法。

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

有问题的特殊问题是导航栏的条形按钮项间隔不同.我的左右主要条形按钮项目现在更接近屏幕的水平中心,我无法将它们移动到屏幕边缘附近.在过去,我使用了自定义UIButton子类并使用自定义视图创建了条形按钮项.取向溶液alignmentRectInsetscontentEdgeInsets,现在我无法管理使用这种方法来产生预期的效果.

编辑:
我已经重新测试了iOS 11 beta 2,问题仍然存在.

编辑2: 我已经重新测试了iOS beta 3,问题仍然存在.



1> 小智..:

现在在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;
                        }
                    }
                }
            }
        }
    }
}


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



2> Josh Bernfel..:

大约两天后,这是我能想到的最简单,最安全的解决方案.此解决方案仅适用于自定义视图栏按钮项,其中包含代码.值得注意的是,导航栏上的左右边距没有从iOS10更改为iOS11 - 它们仍然是16px.如此大的余量使得难以具有足够大的点击区域.

条形按钮现在与UIStackView一起布局.将这些按钮移动到更靠近边缘的现有方法涉及使用这些堆叠视图无法处理的负固定空间.

子类UINavigationBar

FWNavigationBar.h

@interface FWNavigationBar : UINavigationBar

@end

FWNavigationBar.m

#import "FWNavigationBar.h"

@implementation FWNavigationBar

- (void)layoutSubviews {
    [super layoutSubviews];

    if (@available(iOS 11, *)) {
        self.layoutMargins = UIEdgeInsetsZero;

        for (UIView *subview in self.subviews) {
            if ([NSStringFromClass([subview class]) containsString:@"ContentView"]) {
                subview.layoutMargins = UIEdgeInsetsZero;
            }
        }
    }
}

@end

使用UINavigationController

#import "FWNavigationBar.h"

UINavigationController *cOntroller= [UINavigationController initWithNavigationBarClass:[FWNavigationBar class] toolbarClass:nil];
[controller setViewControllers:@[rootViewController] animated:NO];

创建UIBarButton

将此代码放在UIBarButton类别中或使用条形按钮放置在您计划的文件中,该按钮将使用UIButton返回相同的条形按钮项.

+ (UIBarButtonItem *)barButtonWithImage:(UIImage *)image target:(id)target action:(SEL)action {
   UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
   //Note: Only iOS 11 and up supports constraints on custom bar button views
   button.frame = CGRectMake(0, 0, image.size.width, image.size.height);

   button.tintColor = [UIColor lightGrayColor];//Adjust the highlight color

   [button setImage:image forState:UIControlStateNormal];
   //Tint color only applies to this image
   [button setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateHighlighted];
   [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

   return [[UIBarButtonItem alloc] initWithCustomView:button];
}

在控制器中设置条形按钮

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.leftBarButtOnItem= [UIBarButtonItem barButtonWithImage:[UIImage imageNamed:@"your_button_image"] target:self action:@selector(leftButtonPressed)];
}

最后,我建议将左右边距保持为零,只需调整图像中按钮的位置即可.这将允许您利用直到屏幕边缘的完整可点击区域.图像的高度也是如此 - 确保高度为44磅.



3> 小智..:

关于此有好的文章:http : //www.matrixprojects.net/p/uibarbuttonitem-ios11/

使用此方法,我们至少可以将rightbarbuttonitems向右推,直到从UINavigationBar的尾部离开8像素边距为止。

解释得很好。


推荐阅读
author-avatar
裴潇艳
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有