Client error attempting to change layout margins of a private view

1,463 阅读1分钟

项目中使用那个GKNavigationBarViewController 第三方框架,更新了手机系统(iOS 13以上),导航栏的item都会偏离了手机两侧item到屏幕之间有20的边距,但是一直都没有看到GKNavigationBarViewController有更新,很多时候还是靠自己。

使用真机调试还出现了崩溃(系统是iOS 13.1 xcode是11.1 ,xcode 10.3是不会有奔溃现象的,是因为升级了手机才要升级xcode)

奔溃原因:Client error attempting to change layout margins of a private view

GKNavigationBarViewController 崩溃处的代码,

if (GKDeviceVersion >= 11.0 && !GKConfigure.gk_disableFixSpace) {
        self.layoutMargins = UIEdgeInsetsZero;
        for (UIView *subview in self.subviews) {
            if ([NSStringFromClass(subview.class) containsString:@"ContentView"]) {
                // 修复iOS11 之后的偏移
                subview.layoutMargins = UIEdgeInsetsMake(0, self.gk_navItemLeftSpace, 0, self.gk_navItemRightSpace);
                break;
            }
        }
    }

更改私有视图的布局直接崩溃

解决办法:
使用frame的方式,让_UINavigationBarContentView 向两边伸展,从而抵消两边的边距

// 设置导航item偏移量
    if (GKDeviceVersion >= 13.0 && !GKConfigure.gk_disableFixSpace) {
        for (UIView *subView in self.subviews) {
            if ([NSStringFromClass([subView class]) containsString:@"_UINavigationBarContentView"]) {
                UIEdgeInsets margins = subView.layoutMargins;
                subView.frame = CGRectMake(-margins.left, Height_StatusBar, margins.left + margins.right + subView.frame.size.width, margins.top + margins.bottom + subView.frame.size.height);
                break;
            }
        }
    } else if (GKDeviceVersion >= 11.0 && !GKConfigure.gk_disableFixSpace) {
        self.layoutMargins = UIEdgeInsetsZero;
        for (UIView *subview in self.subviews) {
            if ([NSStringFromClass(subview.class) containsString:@"ContentView"]) {
                // 修复iOS11 之后的偏移
                subview.layoutMargins = UIEdgeInsetsMake(0, self.gk_navItemLeftSpace, 0, self.gk_navItemRightSpace);
                break;
            }
        }
    }

GKDeviceVersion 是框架的一个宏
#define GKDeviceVersion [[[UIDevice currentDevice] systemVersion] floatValue]

Height_StatusBar 是我适应app设置的宏,正常是用 -margins.top ,但是在iPhonex系列,UIBarItem 会向上移动44,最后还是采用这种方式

#define SCREEN_HEIGHT [[UIScreen mainScreen]bounds].size.height
#define IS_IPHONE_X (SCREEN_HEIGHT >= 812.0f) ? YES : NO
#define Height_StatusBar ((IS_IPHONE_X==YES) ? 44.0f: 20.0f)