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

时间:2022-08-24 23:26:03

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. 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. Any idea?

我想保存用户离开webView(正在加载本地html文件)的状态,以便下次当他进入相同的视图时,他将被带到他上一次读取的同一行文本中。我认为这可以通过每次用户滑动文档时将滚动值保存为整数来实现,并且当他下次进入视图时我们使用之前保存的值并使用javascript命令或“CG”滚动视图“事情。任何的想法?

2 个解决方案

#1


and an objective c solution:

和客观的解决方案:

@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];
}

#2


From Lyndsey Scott's Answer:

来自Lyndsey Scott的答案:

To save and retrieve your scrollview offset from NSUserDefaults, set your UIWebView's delegate and try this:

要从NSUserDefaults保存和检索您的scrollview偏移量,请设置您的UIWebView的委托并尝试以下操作:

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的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值也会在应用启动之间保持不变。

#1


and an objective c solution:

和客观的解决方案:

@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];
}

#2


From Lyndsey Scott's Answer:

来自Lyndsey Scott的答案:

To save and retrieve your scrollview offset from NSUserDefaults, set your UIWebView's delegate and try this:

要从NSUserDefaults保存和检索您的scrollview偏移量,请设置您的UIWebView的委托并尝试以下操作:

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的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值也会在应用启动之间保持不变。