0
votes

I want user tap the tab bar item to let scroll view scroll to top.

If just set content offset like below, the animation is not same as tapping the status bar to scroll to top in iOS 11.

scrollView.setContentOffset(CGPoint(x: 0, y: -scrollView.adjustedContentInset.top), animated: true)

I found in the App Store app, when user tap the tab bar item, the page will scroll to top, and the animation is same as tapping the status bar, how to archive this animation?

2
Try scrollRectToVisible - Brandon

2 Answers

0
votes

You can hook top constraint of scrollView and do want you want

self.scrollTopCon.constant = // value

UIView.animate(withDuration: 0.5, animations: {

    self.view.layoutIfNeeded()

})
-1
votes
extension UIScrollView {

    @objc public func extension_scrollToTopIfPossible(animated: Bool) {
        let selector = NSSelectorFromString("_scrollToTopIfPossible:")
        guard self.responds(to: selector) else {
            return
        }
        self.performSelector(onMainThread: selector, with: animated ? true : nil, waitUntilDone: true)
    }
}

OR

@implementation UIWindow (SCROLL)

- (void)performScrollToTop {

    SEL selector = NSSelectorFromString(@"_scrollToTopViewsUnderScreenPointIfNecessary:resultHandler:");

    if ([self respondsToSelector:selector] == false) {
        return;
    }

    NSMethodSignature *signature = [UIWindow instanceMethodSignatureForSelector:selector];

    if (signature == nil) {
        return;
    }

    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIWindow instanceMethodSignatureForSelector:selector]];

    [invocation setTarget:self];

    [invocation setSelector:selector];

    CGRect statusBarFrame = UIApplication.sharedApplication.statusBarFrame;

    CGPoint point = CGPointMake(statusBarFrame.size.width / 2.0, statusBarFrame.size.height + 1.0);

    [invocation setArgument:&point atIndex:2];

    [invocation invoke];
}

@end