Set last scroll position to UITextView before view appears

174 views Asked by At

Right now, I have UITextView in BViewController. I save it's scroll position to AViewController when viewWillDisappear with

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

    double d = self.textView.contentOffset.y;
    self.aViewController.lastOffset = [NSNumber numberWithDouble:d];

    NSLog(@"textView contentHeight %f", self.textView.contentSize.height)
}

When user comes back to BViewController, I set UITextView to last scroll position.

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

    // Force layout
    [self.view setNeedsLayout];
    [self.textView.layoutManager ensureLayoutForTextContainer:self.textView.textContainer];
    //[self.view layoutIfNeeded];

    double d = [self.aViewController.lastOffset doubleValue];
    [self.textView setContentOffset:CGPointMake(0, d) animated:NO];

    NSLog(@"textView contentHeight %f", self.textView.contentSize.height);
}

The result is, UITextView's scroll position is off from the last scroll position. NSLog value in viewWillAppear is way less than in viewWillDisappear. Even though I forced it to layout with ensureLayoutForTextContainer or layoutIfNeeded.

I know I get correct result in viewDidAppear or viewDidLayoutSubviews, but the user will be able to see it animating to the last scroll position.

Is it not possible to set subview's scroll position before view appears?

Thanks in advance.

0

There are 0 answers