以编程方式滚动对UIWebView没有影响

时间:2017-07-03 04:34:27

标签: ios uiwebview

我有以下UIWebView实现,我想在加载webview后设置滚动。但是,以编程方式滚动对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);
    });
}

2 个答案:

答案 0 :(得分:2)

你需要在webView中调用它来完成加载网页内容。

不要忘记在ViewController中添加UIWebViewDelegate

  – 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);

      });

    }

答案 1 :(得分:1)

您可以为UIWebview设置委托并尝试以下代码:

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