I know a similar question has been asked before but it seemed never been answered. I have a UIWebView and add some content by string. I use UIWebView because I add some images to it dynamically and also use other HTML features. This example code is simplified.
NSString *myHtmlString = @"SOME LONG TEST STRING 1234567890 123456789 0123456789 0123456789 0123456789 01234567890 WWWWWWWWWW WWWWWWWWWW WWWYYYYYYY YYYYYYYYYY YYYYYYYYYY YYYYYYYYYY YYYYYYYYYY YYYYYYYYY YYYYYYYWWW WWWWWWWWWW WWWWWWWWWW WWWXXXXXXX XXXXXXXXXX XXXZZZZZZZ THIS IS THE END I WANT TO SEE";
NSString *myPath = [[NSBundle mainBundle] bundlePath];
NSURL *myBaseURL = [NSURL fileURLWithPath:myPath];
[myWebView loadHTMLString:myHtmlString baseURL:myBaseURL];
What I want to see is the end of the string. I can scroll there, no problem. But I want to go there programatically.
Это довольно просто. Во-первых, вам нужно получить высоту веб-страницы:
NSInteger height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] intValue];
Теперь у вас есть высота документа, сохраненная в переменной height. Чтобы прокрутить вниз, вам нужно снова использовать javascript:
NSString* javascript = [NSString stringWithFormat:@"window.scrollBy(0, %d);", height];
[webView stringByEvaluatingJavaScriptFromString:javascript];
Конечно, вам нужно вызывать их в нужный момент. Это метод
– webViewDidFinishLoad:(UIWebView *)webView
вашего делегата веб-просмотра.
Надеюсь, это было полезно, Павел