简体   繁体   English

如何在UIWebView中设置内容偏移量和内容大小

[英]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: 我发现解决问题的最好方法是:

  1. Create a UIWebView 创建UIWebView
  2. Put it in a UIScrollView 把它放在UIScrollView
  3. Add the HTML into it using "loadHTMLString" 使用“loadHTMLString”将HTML添加到其中
  4. Using the "- (void)webViewDidFinishLoad:(UIWebView *)webView {" delegate method I detect the size of the UIWebView 使用“ - (void)webViewDidFinishLoad:(UIWebView *)webView {”委托方法我检测UIWebView的大小

  5. Set the frame of the UIWebView to the content size. UIWebView的框架设置为内容大小。

  6. Remove interaction from the UIWebView UIWebView删除交互
  7. Set the Content size of the 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 . 现在,您可以请求UIWebViewscrollView属性的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 . 一旦获得UIWebViewUIScrollView ,就可以轻松更改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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM