2017-07-03 34 views
1

我有以下UIWebView實現,我想在webview加載後設置滾動。但是,以編程方式滾動對UIWebView沒有影響。以編程方式滾動對UIWebView沒有影響

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    NSString *urlAddress = @"http://www.xxxx.com/private-dining"; 

    //Create a URL object. 
    NSURL *url = [NSURL URLWithString:urlAddress]; 

    //URL Requst Object 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 

    //Load the request in the UIWebView. 
    [privateDiningWebView loadRequest:requestObj]; 

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ 
     privateDiningWebView.scrollView.contentOffset = CGPointMake(0, 300); 
    }); 
} 
+0

,你不應該調用'vieDidLaoad'這種方法 – Dhiru

回答

1

您可以設置UIWebView的委託和試試這個代碼:

- (void)webViewDidFinishLoad:(UIWebView *)webView { 
    privateDiningWebView.scrollView.contentOffset = CGPointMake(0, 300); 
} 
2

你需要webView調用,這是完成加載網頁內容。

別忘了在您添加UIWebViewDelegate的ViewController

– webViewDidFinishLoad:(UIWebView *)webView 
    { 
    // UI Operation should be Done in Main Queue so you need to get 
    // main Queue to Access your webView 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     privateDiningWebView.scrollView.contentOffset = CGPointMake(0, 300); 

     }); 

    }