I am writing this in 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];

The web view created here does not align itself to portrait and landscape orientations properly, whereas if the web view is created through Interface Builder, it works fine.

(Not sure if something wrong with initWithFrame:self.view.frame)

What could be the problem?

有帮助吗?

解决方案

You should set the autoresizingMask:

webVPrg.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

to make the view automatically adapt its size according to its superview's. You will possibly need to do the same with your self.view as well.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top