我是iphone开发的新手。在我的应用程序中,我想在设备中显示带有c纵向Orientation的Web视图。它还应该支持横向。我使用了以下方法,但没有预期的输出。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait ||
interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation ==
UIInterfaceOrientationLandscapeRight);
}
请指导我。请帮帮我。谢谢
答案 0 :(得分:10)
我认为你已经为webview设置了固定的框架。这在实现方向chage时无济于事
试试这个:
使用此代码显示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)"];
}
}
希望以上内容能满足您的要求。 一切顺利。
答案 1 :(得分:0)
如果您想支持任何方向,请返回YES
。
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) orientation
{
return YES;
}