解决 iOS 11 webview 顶部空白条的问题
发表于:2024-11-23 作者:热门IT资讯网编辑
编辑最后更新 2024年11月23日,在 iOS 11 使用 webview 发现顶部会有一个空白条,怎么也去不掉,通过设置 contentInsetAdjustmentBehavior = UIScrollViewContentInse
在 iOS 11 使用 webview 发现顶部会有一个空白条,怎么也去不掉,通过设置 contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever 就可以解决了,具体的代码如下:
//// ViewController.m// webView//// Created by exchen on 18/7/14.// Copyright © 2018年 exchen. All rights reserved.// #import "ViewController.h" @interface ViewController ()@property(nonatomic, strong) UIWebView* webView;@end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. CGRect screen = [[UIScreen mainScreen] bounds]; //添加 UIWebView self.webView = [[UIWebView alloc] initWithFrame: CGRectMake(0, 0, screen.size.width, screen.size.height)]; [self.view addSubview: self.webView]; NSURL * url = [NSURL URLWithString: @"https://www.exchen.net"]; NSURLRequest * request = [NSURLRequest requestWithURL:url]; [self.webView loadRequest:request]; self.webView.delegate = self; //解决 iOS 11 屏幕顶部显示不全 if ([[UIDevice currentDevice].systemVersion floatValue] >= 11.0) { self.webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.} - (void)webViewDidStartLoad:(UIWebView *)webView { NSLog(@"%@",NSStringFromSelector(_cmd));} //加载失败时调用- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { NSLog(@"%@",NSStringFromSelector(_cmd)); NSLog(@"error: %@",error); UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"信息" message:@"连接服务器失败,请检查网络" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){ exit(0); }]; [alertController addAction:cancelAction]; [self presentViewController:alertController animated:YES completion:nil];} - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ return true;} @end
原文地址:https://www.exchen.net/%E8%A7%A3%E5%86%B3-ios-11-webview-%E9%A1%B6%E9%83%A8%E7%A9%BA%E7%99%BD%E6%9D%A1%E7%9A%84%E9%97%AE%E9%A2%98.html