通过IB和代码创建的UIWebView对方向不友好



我在viewDidLoad,中写这篇文章

// Programmatically creating uiwebview. 
UIWebView *webVPrg = [[UIWebView alloc] initWithFrame:self.view.frame];
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
NSMutableURLRequest *urlReq = [[NSMutableURLRequest alloc] initWithURL:url];
[self.view addSubview:webVPrg];
[webVPrg loadRequest:urlReq];

此处创建的web视图无法正确地与纵向和横向对齐,而如果web视图是通过界面生成器创建的,则工作正常。

(不确定initWithFrame:self.view.frame是否有问题)

可能是什么问题?

您应该设置autoresizingMask:

webVPrg.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

以使视图根据其超视图自动调整其大小。您可能也需要对self.view执行同样的操作。

最新更新