美文网首页
iOS11 导航栏适配<一>

iOS11 导航栏适配<一>

作者: wustzhy | 来源:发表于2017-10-30 16:13 被阅读303次

看代码

UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                               initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                               target:nil action:nil];
            
            negativeSpacer.width = kNegativeSpacer;  // iOS11 负数失效!, 重写navigationBar解决

            viewController.navigationItem.leftBarButtonItems = @[negativeSpacer,leftButtonItem];

查原因
以上没毛病,但iOS 11貌似出了bug。负宽度的negativeSpacer无效!

找解决

    1. 重写NavigationBar,对我的proj是最小的改动
static CGFloat const kNegativeSpacer = -5; //item边距设置 

- (void)layoutSubviews {
    [super layoutSubviews];
    
    if (@available(iOS 11, *)) {
        self.layoutMargins = UIEdgeInsetsZero;
        
        for (UIView *subview in self.subviews) {
            if ([NSStringFromClass([subview class]) containsString:@"ContentView"]) {
                UIEdgeInsets oEdges = subview.layoutMargins;
                subview.layoutMargins = UIEdgeInsetsMake(0, oEdges.left+kNegativeSpacer, 0, oEdges.right+kNegativeSpacer);
            }
        }
    }
}

    1. 当然还有另一方式
-(void)layoutSubviews{
    [super layoutSubviews];

    if (@available(iOS 11, *)){
      [self updateNavigationBar];
    }
}

- (void) updateNavigationBar {

    for(UIView *view in self.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 = 2.5;
                } else if(ctr.firstAttribute == NSLayoutAttributeTrailing || ctr.secondAttribute == NSLayoutAttributeTrailing) {
                    ctr.constant = -5.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;
                        }
                    }
                }
            }
        }
    }
}
but, 方式2 , 人眼能看到UI的leftItem调整动画,不理想!

参考
https://forums.developer.apple.com/message/238664#238664
https://stackoverflow.com/questions/44677018/ios-11-uinavigationbar-bar-button-items-alignment/46660888#46660888
https://tech.meituan.com/iPhoneX刘海打理指北.html
http://www.jianshu.com/p/383cdad95a32

相关文章

  • iOS11 导航栏适配<一>

    看代码 查原因以上没毛病,但iOS 11貌似出了bug。负宽度的negativeSpacer无效! 找解决 重写N...

  • 升级xcode9 ios11遇到的问题及解决办法

    1. 导航栏titleView适配问题 导航栏搜索视图没有占满导航栏 iOS11 导航栏上的视图推荐使用autol...

  • iOS11及iPhoneX适配

    iOS11及iPhoneX适配 navigation bar适配 1. navigation bar 导航栏的高度...

  • Iphone X 与 IOS 11 适配记录

    一、IOS 11适配 1.1 导航栏 导航栏高度的变化 iOS11之前导航栏默认高度为64pt(这里高度指stat...

  • UIImagePickerController iOS11调起相

    UIImagePickerController iOS11调起相册 中的照片被导航栏遮挡 为了适配iOS11下来刷...

  • 日常问题

    一、iOS11适配问题 1、导航栏问题自定义titleViewiOS11在自定义View上实现: 同样添加导航栏按...

  • iOS 11 适配

    1. iOS11 navigationItem.titleView 适配问题 自定义导航栏 titleView 的...

  • iOS11 和 iPhone X 适配

    Table of Contents iOS11 适配一、Large Title View二、导航栏1. 图层变化2...

  • iOS11 适配方案调研

    iOS11 适配方案调研 一:内容介绍 明确需要适配部分: 1.导航栏 2.滑动view (UIScrollVie...

  • 最简洁适配UINavigtaionBar

    iOS11导航栏适配挺有难度的,出现 美团第三方专场库。提供一个简易的方案, 就是 基础 VC全量隐藏导航栏,定制...

网友评论

      本文标题:iOS11 导航栏适配<一>

      本文链接:https://www.haomeiwen.com/subject/nagppxtx.html