【问题标题】:UIWebBrowserView does not span entire UIWebViewUIWebBrowserView 不跨越整个 UIWebView
【发布时间】:2015-01-25 10:00:56
【问题描述】:

所以一段时间以来,我一直试图让这个简单的行为在我的 iPhone 应用程序上运行一段时间。我在顶部有一个导航栏,在底部有一个标签栏。我正在将我的所有内容加载到一个 webview 中,我希望它介于两者之间。这个问题我已经发过两次了,

都在这里:IOS View still does not load in the correct position

这里:Programmatically setting content to fill screen between tab bar and nav controller

目前,我的 webview 在 iPhone 4 和 4s 上看起来和执行良好,但问题出现在 5s 上。当屏幕完成加载时,它看起来像这里的图像:http://grosh.co/screen.jpg。这发生在模拟器和设备上。

经过进一步检查,我发现 UIWebView 实际上是正确跨越的(您看到的灰色区域是 webview)。较小的内容实际上是一个 UIWebBrowserView,我很难找到任何相关信息。

我有我在下面使用的代码,以及一个日志输出以确保所有数字都是正确的。我的问题是,

1) 有没有更好的方法将 webview 设置为跨越所有设备的顶部和底部栏之间的屏幕区域?

2) 如果没有,有没有办法将 UIWebBrowser 设置为跨越整个 webview?

我尝试迭代 webview 的子视图,如下所述:http://normansoven.com/ios-webview/#.VHyOUr6Jnww 但从未见过 webBrowserView。

法典:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.    


CGFloat viewheight = self.view.bounds.size.height;
NSLog(@"Height1:%f",viewheight);

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat viewheight2 = screenRect.size.height;
NSLog(@"Height2:%f",viewheight2);

CGFloat height = self.view.frame.size.height;
NSLog(@"Height3:%f",height);


CGFloat navBarHeight = self.navigationController.navigationBar.frame.size.height;
NSLog(@"NAVHeight:%f",navBarHeight);

CGFloat tabBarHeight = self.tabBarController.tabBar.frame.size.height;
NSLog(@"TABHeight:%f",tabBarHeight);

CGFloat statusbarheight = [UIApplication sharedApplication].statusBarFrame.size.height;
NSLog(@"StatbarHeight:%f",statusbarheight);

CGFloat spaceToRemove = navBarHeight + tabBarHeight + statusbarheight;
    NSLog(@"NAVHeight+TABHeight:%f",spaceToRemove);
CGFloat newHeight = viewheight - spaceToRemove;
        NSLog(@"NewHeight:%f",newHeight);

CGRect frame =  CGRectMake(0 , navBarHeight + statusbarheight, self.view.frame.size.width, newHeight);

self.webView = [[UIWebView alloc]initWithFrame:frame];


//self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];


[self.view addSubview:self.webView];

self.webView.scalesPageToFit = true;
NSURL *url = [NSURL URLWithString:@"http://example.com/finnaRoot/index.php"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestURL];
self.webView.scrollView.showsVerticalScrollIndicator = false;


self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]];

self.webView.delegate = self;
self.tabBarController.delegate = self;
self.webView.scrollView.delegate = self;

}

日志输出,来自 iPhone 5s

2014-11-26 17:19:21.184 Finna[14617:1765689] Height1:568.000000
2014-11-26 17:19:21.185 Finna[14617:1765689] Height2:568.000000
2014-11-26 17:19:21.185 Finna[14617:1765689] Height3:568.000000
2014-11-26 17:19:21.185 Finna[14617:1765689] NAVHeight:44.000000
2014-11-26 17:19:21.185 Finna[14617:1765689] TABHeight:49.000000
2014-11-26 17:19:21.185 Finna[14617:1765689] StatbarHeight:20.000000
2014-11-26 17:19:21.185 Finna[14617:1765689] NAVHeight+TABHeight:113.000000
2014-11-26 17:19:21.186 Finna[14617:1765689] NewHeight:455.000000

【问题讨论】:

  • 所以最新的 iOS 更新修复了这个问题,这让我相信这是一个错误。有机会时,我将添加我在下面使用的解决方法代码。它起作用了,应用程序被商店接受了。

标签: ios iphone layout uiwebview


【解决方案1】:

这个问题在 iPhone X 上也再次出现。

在 iOS 11 中,UIScrollView 现在具有属性 contentInsetAdjustmentBehavior。

webview.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;

可以纠正这个问题。

【讨论】:

  • 对我来说效果很好,但需要使用@available 进行额外的包装: if (@available(iOS 11.0, *)) { self.webView .scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; }
【解决方案2】:

我也遇到过同样的问题。 并找到了简单的解决方法。 罪魁祸首是UIViewController 的财产automaticallyAdjustsScrollViewInsetsUIViewController 参考说:

默认值为YES,允许视图控制器调整其 滚动视图插入以响应屏幕区域所消耗的 状态栏、导航栏和工具栏或标签栏。

这意味着UIViewController 会将顶部间隙添加到UIWebView 的滚动视图中,即使您正确设置了 webview 框架或添加了自动布局约束。为了克服这种行为,只需

如果你设置为NO 想要自己管理滚动视图插图调整,例如何时 视图层次结构中有多个滚动视图。

“管理您自己的滚动视图插入调整” 在我们的上下文中意味着我们不会添加任何插入;)

任何滚动视图都有同样的问题,即Blank space at top of UITextView in iOS 10 https://stackoverflow.com/search?q=automaticallyAdjustsScrollViewInsets

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 2010-10-07
    • 2020-07-04
    相关资源
    最近更新 更多