ios UIWebView contentInset 底部没有额外的高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17358950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIWebView contentInset without extra height at bottom
提问by Brent Royal-Gordon
I'm creating a view controller that uses a web view which slides behind the navigation bar and status bar. To do this, I'm setting the webView.scrollView.contentInset
property to have a top inset of 64
.
我正在创建一个视图控制器,它使用一个在导航栏和状态栏后面滑动的 web 视图。为此,我将webView.scrollView.contentInset
属性设置为顶部插入64
.
However, this doesn't shrink the amount of area the web view wants to take up, so if a page is less than a screenful, it has 64 px of white space at the bottom to scroll through. The web views are in a vertical UIPageViewController
, so this disrupts paging. Is there some way to get rid of this extra space?
但是,这不会缩小 web 视图想要占用的区域量,因此如果页面小于一屏,则底部有 64 px 的空白区域可以滚动。Web 视图是垂直的UIPageViewController
,因此这会中断分页。有没有办法摆脱这个额外的空间?
采纳答案by Brent Royal-Gordon
I've sort of made an end-run around this problem by disabling scrolling on short pages. Basically, I use -stringByEvaluatingJavaScriptFromString:
to run some JavaScript inside the page that walks down from document.body
and computes the position of the bottom of the bottommost element. (document.body
itself is always at least as large as the viewport, so I can't look at its size.) Then, back in Objective-C-land, I compare that to the height of the web view (less the inset), and if it's less, I disable scrolling. This is not a perfect solution, but it covers the worst symptoms.
我通过在短页面上禁用滚动来解决这个问题。基本上,我使用-stringByEvaluatingJavaScriptFromString:
在页面内运行一些 JavaScript,从document.body
最底部元素的底部向下走并计算其位置。(document.body
它本身总是至少与视口一样大,所以我无法查看它的大小。)然后,回到 Objective-C-land,我将其与 Web 视图的高度(减去插图)进行比较,然后如果它更少,我会禁用滚动。这不是一个完美的解决方案,但它涵盖了最糟糕的症状。
If someone can come up with a better solution than this, I'd love to hear about it! Either way, I can't award the bounty to myself, so someone will be getting it.
如果有人能想出比这更好的解决方案,我很想听听!无论哪种方式,我都不能将赏金奖励给自己,所以有人会得到它。
回答by royherma
Have you tried something like adjusting the webview's scrollview content insets? Example:
您是否尝试过调整 webview 的滚动视图内容插入之类的方法?例子:
webView.scrollView.contentInset = UIEdgeInsetsMake(0, 0,64, 0)
回答by Chris Truman
It sounds like what you need is to adjust the webView.scrollView.contentSize
and adjust the height by 64. You may need to provide more information about how it slides behind the nav bar and status bar to help me answer this. I would take a look at this section of the Scroll View Programming Guide:
听起来您需要的是webView.scrollView.contentSize
将高度调整和调整 64。您可能需要提供有关它如何在导航栏和状态栏后面滑动的更多信息,以帮助我回答这个问题。我会看看滚动视图编程指南的这一部分:
回答by Maximilian Litteral
Change the clipsToBounds.
更改 clipsToBounds。
webView.clipsToBounds = NO;
This will make its content visible outside its frame, so set its frame like normal, right under the navigation bar. The navigation bar will be translucent and you will see its content under it.
这将使其内容在其框架外可见,因此将其框架设置为正常,就在导航栏下方。导航栏将是半透明的,您将在其下方看到其内容。
回答by KiRviz
Don't forget to disable Autolayout for the web view!
不要忘记禁用 Web 视图的自动布局!