iOS webview leaves empty space before HTML content

2019-05-22 15:03发布

I have the simplest view. A webview with some static HTML code embedded using the loadHTMLStringmessage. The only thing I've modifed in the webview's properties from the storyboard is the "Scale page to fit" property (it's checked). However, as you can see in the image below, there's an empty space above the HTML that belongs to the webview (if I scroll the content it fills up that space). I've even tried just using...

<html>
<body><h1>Hello</h1></body>
</html>

... but the space is still there (so I don't think it has anything to do with the HTML code).

How can I remove that space?

enter image description here

标签: ios uiwebview
2条回答
叼着烟拽天下
2楼-- · 2019-05-22 16:03

iOS adjusts the content inset only for the first view in the view hierarchy if it is UIScrollView or it's descendant (e.g. UITableView and UICollectionView, in your case its a UIWebView). If your view hierarchy includes multiple scroll views, automaticallyAdjustsScrollViewInsets will make adjustments only to the first one.

Here's how to change this behavior:

a) Interface Builder

Select the view controller Open Attributes inspector There's a property called "Adjust scroll view insets" in IB's attribute inspector (when a ViewController is selected) which is on by default.

enter image description here

b) Programmatically

Add this to i.e. viewDidLoad method

Obj-C: self.automaticallyAdjustsScrollViewInsets = NO;

Swift: self.automaticallyAdjustsScrollViewInsets = false

查看更多
欢心
3楼-- · 2019-05-22 16:04

Turn off adjusts scroll view insets.It's here: enter image description here

查看更多
登录 后发表回答