在 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 () <UIWebViewDelegate> @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
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。