方案1:
AppDelegate文件
didFinishLaunchingWithOptions()中添加如下代码
if (@available(iOS 11.0, *)) { [[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever]; }
方案2:
若按照<方案1>设置完依然存在UIWebView位置下移20pt的问题,那么在根控制器(vc、navigationController、tabBarController)的viewDidLoad()中添加如下代码
if (@available(iOS 11.0, *)) { self.additionalSafeAreaInsets = UIEdgeInsetsMake(-20, 0, 0, 0); }
方案3: (推荐)
- (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; // 适配iOS_11中UIWebView内容下移20的问题 if (@available(iOS 11.0, *)) { // safeAreaSetted通过一个BOOL属性来标记是否已适配 if (safeAreaSetted) { return; } CGFloat top =self.view.safeAreaInsets.top; [self.web_main mas_updateConstraints:^(MASConstraintMaker *make) { make.top.mas_offset(-top); safeAreaSetted = YES; }]; } }
以上。