原创首次解决
if (iPhoneX){ for (id object in [self.navigationController.navigationBar subviews]) { if ([NSStringFromClass([object class]) \nisEqualToString:@"_UIBarBackground"]) { // 对 object 进行了判断,它一定是 UIView 或其子类 UIView * view = (UIView *)object; // 对 view 进行操作 // 因为 view 一定是 UIView,所以绝对安全 UIView * vw = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 64+24)]; vw.backgroundColor = MainNavBarColor; [view addSubview:vw]; } } }