[英]How to set content offset and content size in a UIWebView
I have had to change a UIScrollView
into a UIWebView
due to formatting reasons. 由于格式化原因,我不得不将
UIScrollView
更改为UIWebView
。
There is a problem though that I need to be able to get the content offset back to 0, 0
(as I am using reusable UIWebView
s and changing content) and get the content size so I can place a button at the bottom; 虽然我需要能够将内容偏移量恢复到
0, 0
(因为我正在使用可重复使用的UIWebView
并更改内容)并获取内容大小以便我可以在底部放置一个按钮,但是存在一个问题。 but UIWebView
does not seem to have this. 但
UIWebView
似乎没有这个。
This problem can be solved quite easily: 这个问题很容易解决:
[[webView scrollView] setContentInset:UIEdgeInsetsMake(64, 0, 44, 0)];
Remember that UIEdgeInset is different from CGRect! 请记住,UIEdgeInset与CGRect不同!
A guide on UIEdgeInset can be found in the iOS Developer Library . 可以在iOS Developer Library中找到UIEdgeInset指南。
The best way I found to sort the problem was: 我发现解决问题的最好方法是:
UIWebView
UIWebView
UIScrollView
UIScrollView
Using the "- (void)webViewDidFinishLoad:(UIWebView *)webView {" delegate method I detect the size of the UIWebView
使用“ - (void)webViewDidFinishLoad:(UIWebView *)webView {”委托方法我检测
UIWebView
的大小
Set the frame of the UIWebView
to the content size. 将
UIWebView
的框架设置为内容大小。
UIWebView
UIWebView
删除交互 UIScrollView
to the content size. UIScrollView
的内容大小设置为内容大小。 Then used the functionality of the UIScrollView
instead of the UIWebView
. 然后使用
UIScrollView
的功能而不是UIWebView
。
Thanks -JM 谢谢-JM
Use JavaScript. 使用JavaScript。
I think that the next line should do the magic: 我认为下一行应该有魔力:
[webView stringByEvaluatingJavaScriptFromString:@"window.scroll(0,0)"];
Now you can ask for the contentSize
property of the scrollView
property of a UIWebView
. 现在,您可以请求
UIWebView
的scrollView
属性的contentSize
属性。 This enables you to also change the content size of that web view, using CGSizeMake (width, height)
. 这使您还可以使用
CGSizeMake (width, height)
更改该Web视图的内容大小。
Once you get the UIScrollView
that is inside the UIWebView
, it will be easy to change ContentOffset and contentSize
. 一旦获得
UIWebView
的UIScrollView
,就可以轻松更改ContentOffset和contentSize
。 Information here on how to get the scrollView : ScrollOffset in UIWebView? 有关如何在UIWebView中获取scrollView: ScrollOffset的信息?
适合我
[[webView scrollView] setContentInset:UIEdgeInsetsMake(64, 0, 44, 0)];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.