gpt4 book ai didi

iphone - Webview 在 iPhone 中自动调整为纵向和横向 View

转载 作者:行者123 更新时间:2023-12-03 18:43:02 27 4
gpt4 key购买 nike

我是 iPhone 开发新手。在我的应用程序中,我想以 C 纵向方向在设备中显示 Web View 。它还应该支持横向方向。我使用了以下方法,但没有预期的输出。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{

return (interfaceOrientation == UIInterfaceOrientationPortrait ||

interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation ==

UIInterfaceOrientationLandscapeRight);
}

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

最佳答案

我认为您已经为 webview 设置了固定框架。这在实现方向变化时没有帮助

试试这个:

使用此代码显示 Web View 。将堆栈溢出 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;

}

对于随方向变化的 WebView

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

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

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

关于iphone - Webview 在 iPhone 中自动调整为纵向和横向 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2325795/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com