views:

762

answers:

2

Hi

I am new to iphone development.In my app, i want to display the web view in device with c portrait Orientation.It should also support landscape orientation. I used the below method but there is no expected output.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{

return (interfaceOrientation == UIInterfaceOrientationPortrait || 

 interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == 

UIInterfaceOrientationLandscapeRight);
}

please guide me.Please help me out.Thanks

A: 

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

- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) orientation 
{
    return YES;
}
St3fan
+3  A: 

I think you have set the fixed frame for webview.That will not help while implementing orientation chage

Try This:

Use this code for displaying the web-view.Change the stack-overflow URL with your 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;

}

For web-view change with orientation

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

}
else{
    [webView stringByEvaluatingJavaScriptFromString:@"rotate(1)"];
}
}

Hope the above will satisfy your requirement. All the best.

Warrior
Thanks. I want to display the toolbar in my webview? how can i achieve this?. i am not able to see the toolbar in my webview.
Pugal Devan
@pugal Insert the tab bar as subview ... [self.view insertSubview:tBar belowSubview:contentView];change tBar to the toolbar you have used.
Warrior