2010-02-24 20 views
6

我是iphone開發新手。在我的應用程序中,我想顯示設備中的Web視圖與c肖像Orientation.It應該也支持橫向。我使用下面的方法,但沒有預期的輸出。Webview自動調整大小,並自動調整到肖像和風景視圖iphone

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 

return (interfaceOrientation == UIInterfaceOrientationPortrait || 

interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == 

UIInterfaceOrientationLandscapeRight); 
} 

請指導me.Please幫我out.Thanks

回答

10

我想你已經爲webview.That固定框架,同時實施定向CHAGE

試試這不會幫助:

使用此代碼用於顯示網頁view.Change堆棧流量網址與您的網址;

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]; 

爲了支持方向

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

} 

對於Web視圖變化與方向

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

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

希望上述將滿足您的要求。 一切順利。

+0

謝謝。我想在我的webview中顯示工具欄?我怎麼能做到這一點?我無法在我的webview中看到工具欄。 – Pugal 2010-02-25 12:51:21

+0

@pugal插入標籤欄作爲子視圖... [self.view insertSubview:tBar belowSubview:contentView]; 將tBar更改爲您已使用的工具欄。 – Warrior 2010-02-25 12:57:07

0

如果你想支持任何方向然後就返回YES

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

這在iOS 6中已被棄用:https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/DeprecationAppendix/AppendixAdeprecatedAPI.html#jumpTo_9 – poshaughnessy 2013-10-17 15:57:42