Webview automatically resizes in portrait and landscape mode on iphone

I am new to iphone development. In my application, I want to display a web view on a device with a focus on port c. It should also support landscape orientation. I used the method below, but there is no expected result.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight); } 

please guide me. Please help me. thanks

+6
iphone uiwebview screen-orientation landscape
source share
2 answers

I think you set a fixed frame for web browsing. This will not help when implementing the chage orientation

Try the following:

Use this code to display a web view. Change the url to url;

 contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); self.view = contentView; self.view.autoresizesSubviews = YES; CGRect webFrame = [[UIScreen mainScreen] applicationFrame]; webFrame.origin.y -= 20.0; webView = [[UIWebView alloc] initWithFrame:webFrame]; [contentView addSubview:webView]; webView.scalesPageToFit = YES; webView.autoresizesSubviews = YES; webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth); [webView setBackgroundColor:[UIColor clearColor]]; NSString *urlAddress = @"https://www.stackoverflow.com"; NSURL *url = [NSURL URLWithString:urlAddress]; NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; [webView loadRequest:requestObj]; webView.delegate =self; [webView release]; 

To support orientation

  - (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) orientation { return YES; } 

To change the orientation web view

 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { if(fromInterfaceOrientation == UIInterfaceOrientationPortrait){ [webView stringByEvaluatingJavaScriptFromString:@"rotate(0)"]; } else{ [webView stringByEvaluatingJavaScriptFromString:@"rotate(1)"]; } } 

We hope that the above will satisfy your requirement. All the best.

+10
source share

If you want to support any orientation, just return YES .

 - (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) orientation { return YES; } 
0
source share

All Articles