UIKit: UIScrollView automatically scrolling when a subview increases its width past the edge of the screen

后端 未结 3 1091
迷失自我
迷失自我 2021-02-13 13:09

In the context of the iPhone:

I have a UIScrollView containing a UIImage. When the user taps the screen inside the UIImage, a

3条回答
  •  忘了有多久
    2021-02-13 14:01

    Basically setFrame of UIScrollView will readjust the scrollview offset, done by _adjustContentOffsetIfNecessary. As that method is private and not documented, there is very little we can guess on how the adjustment will happen. There are two ways to stop the unnecessary scrolling or wrong offset being set:

    1) reset the UIScrollView offset after applying setFrame. This you can do, if you are modifying the frame of UIScrollView intentionally based on some calculations.

    CGPoint oldOffset = [scrollView contentOffset];         
    scrollView.frame = CGRectMake(newFrame);
    [scrollView setContentOffset:oldOffset];        
    

    2) apply offset changes without animation. In your keyboardWasShown , change [self setContentOffset:scrollPoint animated:YES]; to
    [self setContentOffset:scrollPoint animated:NO];

    Reason: when more than one offset is applied with animation on, the result offset is ambiguous. Here the internal method(_adjustContentOffsetIfNecessary) applies an offset change and the other one is done by your code. You can notice this if you try to log all the offsets being applied in the UIScrollView delegate method:

    -(void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        NSLog(@" offset: %@", NSStringFromCGPoint(scrollView.conentOffset))
    }
    

    Let me know if this helps.

提交回复
热议问题