修改
UIWebView首先在FirstViewController
中初始化,如下所示:
- (void)viewDidLoad {
[super viewDidLoad];
self.webView = [[UIWebView alloc] initWithFrame:self.view.frame];
self.webView.delegate = self;
[self.view addSubview: self.webView];
NSURL *url = [NSURL URLWithString:BASE_URL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:urlRequest];
}
对此UIWebView
的引用会传递给SecondViewController
,我会将其添加为子视图,如下所示:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.webView.delegate = self;
[self.view addSubview:self.webView];
}
这会将UIWebView附加到View Controller的视图中。如果我在viewDidAppear
中调用此函数,我会得到一个适合整个屏幕的UIWebView。但是,如果我在viewWillAppear
内调用它,UIWebView只占整个屏幕大小的一半。我尝试比较viewController.view
和viewWillAppear
的{{1}}尺寸,但它们似乎相同。
根据这个非常相似的post,适合调整viewDidAppear
中的子视图大小。所以我尝试了以下内容:
viewDidLayoutSubviews
但它看起来仍然如此:
答案 0 :(得分:1)
你不应该在viewWillAppear
中调整UIView的大小。它将在Interface Builder中再次调整大小。如果您不想加入viewDidAppear
,可以尝试输入viewDidLayoutSubviews
答案 1 :(得分:1)
您可以在UIWebView
内设置viewDidLoad
,如下所示:
self.webView = [[UIWebView alloc] initWithFrame:self.view.frame];
self.webView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.webView.delegate = self;
[self.view addSubview:self.webView];
或调整其在didLayoutSubviews
内的大小,如@anhtu所说:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.webView.frame = self.view.bounds;
}
答案 2 :(得分:0)
尝试这些..
UIView *viewAll=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:viewAll];
我希望它有所帮助......