1. 程式人生 > >UIWebView顯示網頁沒有置頂顯示

UIWebView顯示網頁沒有置頂顯示

使用UIWebView顯示網頁時沒有置頂顯示,不僅UIWebWiew有這種情況,UITableView也會出現這種情況,處理起來是一樣的。如下圖所示:


在UIViewController初始化時新增程式碼:self.automaticallyAdjustsScrollViewInsets = NO;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _webView.frame = self.view.frame;
    self.automaticallyAdjustsScrollViewInsets = NO;
    _webView.delegate = self;
    NSLog(@"url = %@", _url);
    if(_url != nil){
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_url]];
        [_webView loadRequest:request];
    }
}
修改後的效果:

給ViewController設定標題可以用以下程式碼:self.navigationItem.title = @"框架程式碼用例"; 或 self.title = @"框架程式碼用例";

//
//  ViewController.m
//  FrameworkUtils
//
//  Created by dcr on 2017/4/15.
//  Copyright © 2017年. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

@implementation ViewController

#pragma mark *******View lifeCycle********

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //通過這個ID得到storyboard中的cell
    NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.textLabel.text = [NSString stringWithFormat:@"UITableViewCell %d", indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self selectItem:indexPath.row];
}

/**
 點選的Item
 **/
- (void)selectItem:(int)row{
    NSLog(@"selectItem row = %d", row);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _tableView.delegate = self;
    _tableView.dataSource = self;
    self.view.backgroundColor = [UIColor whiteColor];
    //設定標題,可用以下兩種方法
    self.navigationItem.title = @"框架程式碼用例";
    //self.title = @"框架程式碼用例";
    //要把這個設定為false,否則在TableView不會置頂顯示
    self.automaticallyAdjustsScrollViewInsets = NO;
    NSLog(@"height = %g, width = %g", SCREEN_SIZE.height, SCREEN_SIZE.width);
    NSLog(@"str = %@", [BackView getString]);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end