简体   繁体   中英

iOS 8 Swift UIWebView content size

I have an UIPageViewController with UIWebViews rendering .pdf as content. The UIWebView (content) is scaled to fit the frame width. Some PDFs have double width so I tried to increase the frame width of the WebView

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    var frame = self.mPageWebview.frame;
    frame.origin.x = 0;
    frame.origin.y = 0;

    if(frame.size.width > self.view.frame.size.width) {
        frame.size.width = self.view.frame.size.width * 2
        frame.size.height = self.view.frame.size.height
    }

    self.mPageWebview.frame = frame
}

For the rendering it works as intended (the frame just shows the left half of the PDF) but scrolling to the right does not work. Can anyone help me with this?

For a scrollview to scroll, the contentview must be larger than the size of the scrollview. So, when you say you scaled the contentview's width to be equal to the frame's width, it won't scroll horizontally as the widths are the same.

Wait until the content has loaded then check its size and fit view to it, as long as the content is bigger than the view it will scroll.

func webViewDidFinishLoad(webView: UIWebView) {

    // Set webView frame to content size. 
    self.webViewHeightConstraint.constant = webView.scrollView.contentSize.height

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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