简体   繁体   中英

UIScrollView setZoomscale does not work from ViewWillApper

Hey guys I googled around a bit, and read some Topics here on stack overflow but no one cloud help me really , so i hope you can help me. I have an UIWebView which display an local saved PDF which is working very fine, but i also want to set the zoom level when the PDF is loaded. I solved it this way.

 override func viewDidAppear(animated: Bool) {

    WebView.scalesPageToFit = true
    if(defaults.objectForKey("positionY") != nil){
        WebView.scrollView.setZoomScale(defaults.objectForKey("zoomlevel") as! CGFloat, animated: false)
        let offest = CGPoint(x: defaults.objectForKey("positionX") as! CGFloat, y: defaults.objectForKey("positionY") as! CGFloat)
        WebView.scrollView.setContentOffset(offest, animated: false)

        print(defaults.objectForKey("zoomlevel") as! CGFloat)
    }

But this is not really nice because, it looks bad to the user, so i tried to move my Code to ViewWillAppear, but then it does not work any more, and the pdf is not zoomed. Thanks for any solution ideas :)

Edit as i wrote ia comment below i printed the value of the Zoomscale after seting it.

        WebView.scrollView.setContentOffset(offest, animated:false)
        println(defaults.objectForKey("zoomlevel") as! CGFloat)
        println(WebView.scrollView.zoomScale)

If i do this in ViewWillAppear or ViewDidLoad, the first value ist the saved value like 1.82366, the second value is still 1.0. If i call this in ViewDidAppear the first and second value are equal except of the First appearance of the View (app start). So if it start the App it is the Same like described above for ViewWillAppear but if i come back from another ViewController via segue it works for ViewDidAppear.

I know this question is probably too old now, but I finally solved it in my swift code, I will put it here it may be useful for others: for webview.scrollview.setzoomscale to work, the zoomscale should be between maximumzoomscale and minimumzoomscale properties. both of these are 1 by default that's why the zooming does not work for you.Add this and it should be fine:

webView.scrollview.maximumZoomScale = CGFloat(10) // or any number higher than your zoomscale

and this should work now.

Setting content offset before view is laid out has no effect at all. To fix it you must call layoutIfNeed() method to immediately lay out the specific view

override func viewWillAppear(animated: Bool) {

    WebView.layoutIfNeeded()
    WebView.scalesPageToFit = true
    if(defaults.objectForKey("positionY") != nil){
        WebView.scrollView.setZoomScale(defaults.objectForKey("zoomlevel") as! CGFloat, animated: false)
        let offest = CGPoint(x: defaults.objectForKey("positionX") as! CGFloat, y: defaults.objectForKey("positionY") as! CGFloat)
        WebView.scrollView.setContentOffset(offest, animated: false)

        print(defaults.objectForKey("zoomlevel") as! CGFloat)
    }
}  

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