简体   繁体   English

如何以编程方式滚动到UIWebView中的一行?

[英]How to scroll programmatically to a to a line in UIWebView?

I want to save the state where the user left the webView (which is loading a local html file) so that the next time when he enters the same view, he is taken to the same line of text that he was reading the previous time. 我想保存用户离开webView的状态(正在加载本地html文件),以便下次他进入相同视图时,将他带到上一次阅读的同一行文本中。 I think that could be achieved by saving the scroll value in an integer every time the user swipe the document, and when he enters the view the next time we use the value that was saved previously and scroll the view using a javascript command or "CG" things. 我认为,可以通过以下方式实现此目的:每次用户滑动文档时,将滚动值保存为整数,当下次用户进入视图时,我们将使用之前保存的值,并使用javascript命令或“ CG”滚动视图”的东西。 Any idea? 任何想法?

From Lyndsey Scott's Answer: 林德西·斯科特的答案:

To save and retrieve your scrollview offset from NSUserDefaults , set your UIWebView's delegate and try this: 为了保存和检索您的滚动视图从偏移NSUserDefaults ,请将您UIWebView's委托,并试试这个:

var viewLaidoutSubviews = false // <-- variable to prevent the viewDidLayoutSubviews code from happening more than once

// Save your content offset when the view disappears
override func viewWillDisappear(animated: Bool) {
var userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setValue(NSStringFromCGPoint(myBrowser.scrollView.contentOffset), forKey: "scroll_offset")
userDefaults.synchronize()

viewLaidoutSubviews = false
}

// Retrieve and set your content offset when the view re-appears
// and its subviews are first laid out
override func viewDidLayoutSubviews() {

if (!viewLaidoutSubviews) {
    // If a scroll_offset is store in NSUserDefaults, use it
    var userDefaults = NSUserDefaults.standardUserDefaults()
    var scrollOffset:CGPoint? = CGPointFromString(userDefaults.valueForKey("scroll_offset") as? NSString)
    if let position:CGPoint = scrollOffset {
        myBrowser.scrollView.delegate = self
        myBrowser.scrollView.setContentOffset(position, animated: false)
    }
    viewLaidoutSubviews = true
}
}

And utilize your UIWebView's webViewDidFinishLoad delegate method to update the scroll offset in the case that the web view didn't finish rendering before the view laid out its subviews: 如果Web视图在布局其子视图之前还没有完成渲染,请利用UIWebView's webViewDidFinishLoad委托方法来更新滚动偏移量:

// Retrieve and set your content offset once the web view has
// finished rendering (in case it hasn't finished rendering
// by the time the view's subviews were laid out)
func webViewDidFinishLoad(webView: UIWebView) {

// If a scroll_offset is store in NSUserDefaults, use it
var userDefaults = NSUserDefaults.standardUserDefaults()
var scrollOffset:CGPoint? = CGPointFromString(userDefaults.valueForKey("scroll_offset") as? NSString)
if let position:CGPoint = scrollOffset {
    myBrowser.scrollView.delegate = self
    myBrowser.scrollView.setContentOffset(position, animated: true)
}
}

But note, your NSUserDefaults value will also persist between app launches unless you delete it. 但是请注意,除非删除它,否则NSUserDefaults值还将在应用程序启动之间保持不变。

and an objective c solution: 和客观的c解决方案:

@interface ViewController () <UIWebViewDelegate>

@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://yourwebsite.com"]]];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    if ([userDefaults objectForKey:@"contentOffset"]) {
    webView.scrollView.contentOffset = CGPointFromString([userDefaults objectForKey:@"contentOffset"]);
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:NSStringFromCGPoint(self.webView.scrollView.contentOffset) forKey:@"contentOffset"];
    [userDefaults synchronize];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM