解决 iOS 11 webview 顶部空白条的问题
admin
2023-02-09 10:00:11
0

在 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

相关内容

热门资讯

赛里木湖景区通报“工作人员殴打... 2026年7月23日17时许,赛里木湖景区入口处发生多名景区工作人员殴打旅游车司机事件,我们深感痛心...
继英伟达黄仁勋后,AMD苏姿丰... IT之家 7 月 24 日消息,随着中国开源 AI 模型的不断变强,近期美国业界出现了关于 AI 是...
辰奕智能获得实用新型专利授权:... 证券之星消息,根据天眼查APP数据显示辰奕智能(301578)新获得一项实用新型专利授权,专利名为“...
湖南湘江新区文化创意产业出海观... 中新网长沙7月23日电(胡语桐)在日本大型量贩门店的货架上,中国研发的视频创意软件实体产品陈列其中;...
中国数学家首次获得的菲尔兹奖“... 当地时间7月23日上午,2026年国际数学家大会在美国费城开幕,现场揭晓2026年菲尔兹奖得主。中国...
猪周期反转,这次可能来真的 市场过去几年关于猪周期的很多认知,现在需要重新审视了。过去几年,越来越多人相信:随着规模化养殖企业不...
王虹、邓煜为啥能获菲尔兹奖? 当地时间7月23日早晨,2026年国际数学家大会在美国费城开幕。国际数学联盟在开幕式上正式公布第二十...
高温津贴,不能用绿豆汤冲抵 近日,深圳市人力资源和社会保障局发布关于高温津贴的典型劳动仲裁案例,并普法提醒:即便劳动合同约定日薪...
菲律宾,你到底想干什么 中国和菲律宾关系,又出幺蛾子了。反正,最近几天,麻烦不断,场面火爆。仁爱礁,菲两艘小艇冲撞我巡逻船只...
聚焦长期价值,方舟云康以数智化... 来源:江南时报 在慢病管理领域,单纯追逐“流量”的互联网医疗模式已显疲态。当线上获客成本持续走高、用...