5

I have embedded a Youtube video in my iOS 8.3 app using youtube's standard embed url (sample) which is working as expected but looking pretty weird. That white gap (blue zone on the view tree inspector) should not be there.

View tree

I can confirm it's not a CSS issue as the inspector shows it is effectively occupying 100% width and height of the uiWebView component.

enter image description here

I have setup constraints to the left, top and right borders to equate the window/layout guides so that's not the problem either. It seems a private _UIWebViewScrollView component is taking all the height i assigned to the UIWebView, and pushing the real content (UIWebViewBrowserView) to the bottom of said area.

ViewTree UIWebViewBrowserView detail

I looked up info on the scroll view class but it seems there's only header files scattered on the web with no real apple docs so i'd rather not mess with that scroll view if possible.

Seems my hunch is right, the same problem was described by someone on another question also regarding a scrollview but the mentioned function to fix the problem either does not exist anymore or is now private. I'm still looking for answers.

4
  • do you give the webview a fixed height? Commented Apr 26, 2015 at 22:00
  • I'm pretty new to iOS dev but the storyboard UIWebView has a 270px height. Just in case here are screenshots of the layout properties for the UIWebView imgur.com/enz84l8,OMh3Qsf Commented Apr 26, 2015 at 22:09
  • no worries. :) click the edit button next to top space to: top layout guide and set the constant to 0. Commented Apr 26, 2015 at 22:11
  • It was already set to 0 :( Commented Apr 26, 2015 at 22:18

4 Answers 4

17

I fixed it.

  override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews();

    playerWebView.scrollView.contentInset = UIEdgeInsetsZero;
  }

Adding to the edit i made to the question, i forgot swift changes set and get methods for accessor variables as in C#.

1
  • 1
    In Swift 3.0 it is now playerWebView.scrollView.contentInset = UIEdgeInsets.zero Commented Feb 22, 2017 at 3:59
2

Swift 3: Removing the default blank top space in UIWebView

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews();

    webView.scrollView.contentInset = UIEdgeInsets.zero;
}
1

The accepted solutions did not work for me. Instead it was just because there was space for the navigation bar title text that I was not using. I got rid of it by adding displayMode: .inline.

    var body: some View {
        VStack {
            WebView(request: URLRequest(url: URL(string: url!)!))
        }.navigationBarTitle(Text("Title"), displayMode: .inline)
    }
0

Update for swift 3

webViewResetPassw.scrollView.contentInset = UIEdgeInsets.zero

Not the answer you're looking for? Browse other questions tagged or ask your own question.