6

我是 iphone 开发的新手。在我的应用程序中,我想在具有 c 纵向方向的设备中显示网络视图。它还应该支持横向方向。我使用了以下方法,但没有预期的输出。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{

return (interfaceOrientation == UIInterfaceOrientationPortrait || 

 interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == 

UIInterfaceOrientationLandscapeRight);
}

请指导我。请帮助我。谢谢

4

2 回答 2

11

我认为您已经为 webview 设置了固定框架。这在实现方向更改时无济于事

尝试这个:

使用此代码显示 web 视图。使用您的 URL 更改堆栈溢出 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];

支持方向

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

}

用于随方向更改的 web 视图

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

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

希望以上内容能满足您的要求。一切顺利。

于 2010-02-25T10:19:40.377 回答
0

如果您想支持任何方向,则只需 return YES

- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) orientation 
{
    return YES;
}
于 2010-02-25T01:50:08.683 回答