0

Hey i asked similar question some Days ago but no on could help me and no i'm think im closer to the problem. I have an WebView on my rootViewController and saved some Values for zoom scale and position of an local PDF, now i want set the Webview to this values when it appears so i tried the following.

   override func viewDidAppear(animated: Bool) {

    doLayoutThings()


}
    func doLayoutThings(){

    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)
        println(defaults.objectForKey("zoomlevel") as! CGFloat)




    }

I know that there are the correct values are saved to UserDefaults but the Webview looks if i didn't make any scrolling or zooming. I'm really happy about any solution Ideas.

Nick
  • 1,417
  • 1
  • 14
  • 21
Bierbarbar
  • 1,399
  • 15
  • 35

2 Answers2

1

I am used this link to resolved this issue

In Swift

Initially i am try to change the web view zoom scale but it was not so working fine .

webview.scrollView.setZoomScale(1.5, animated: true)

so i try to change it in webViewDidFinishLoad

func webViewDidFinishLoad(_ webView: UIWebView) {
    webview.scalesPageToFit = true

    let str = "var meta = document.createElement('meta');"
    let str1 = "meta.setAttribute( 'name', 'viewport' ); "
    let str2 = "meta.setAttribute( 'content', 'width = device-width, initial-scale = 1.0, user-scalable = yes' ); "
    let str3 = "document.getElementsByTagName('head')[0].appendChild(meta)"

    webview.stringByEvaluatingJavaScript(from: "\(str)\(str1)\(str2)\(str3)")

}

if you need to change the zoom level, change the "initial-scale" to see the effect of initial level zooming. It's really helpful for me. Thanks "UIWebView set initial zoom scale programmatically when loading an external website?"

Hari Narayanan
  • 754
  • 1
  • 12
  • 36
0

Setting the zoom scale in viewDidAppear is fine but you cannot set the contentOffset before the web view has loaded its content. So you have to set a delegate on the UIWebView and then set the contentOffset in the delegate method:

override func viewDidLoad() {
   webView.delegate = self
}

func webViewDidFinishLoad(webView: UIWebView) {
    let offset = CGPoint(x: defaults.objectForKey("positionX") as! CGFloat, y: defaults.objectForKey("positionY") as! CGFloat)
    webView.scrollView.setContentOffset(offset, animated: false)
}
joern
  • 27,354
  • 7
  • 90
  • 105
  • here whats the value of "defaults" i am getting this error "Use of unresolved identifier 'defaults'" – Hari Narayanan Nov 17 '17 at 09:26
  • @HariNarayanan The OP stores the scroll position in the UserDefaults to save them. Then when the web view is loaded the next time it gets the stored values for the contentOffset and applies them to the web view – joern Nov 17 '17 at 10:03
  • i got another answer and i resolved it thank you.. from this https://stackoverflow.com/questions/8028051/uiwebview-set-initial-zoom-scale-programmatically-when-loading-an-external-websi – Hari Narayanan Nov 17 '17 at 10:17