复制 UIScrollView 的 scrollToTop 不扩展 UITabBar tap in iOS 11 上的 UINavigationBar

Replicate UIScrollView's scrollToTop does not expand the UINavigationBar on UITabBar tap in iOS 11

(类似于此问题,未回答:Tableview scroll to top not going all the way, as well as this one, also unanswered: Show navigation bar's large title and search bar on scroll to top collection view iOS 11 Swift 4

我试图在点击状态栏时复制滚动到 UITableView 的顶部,但是当我点击 UITabBar 项目并且我已经在该视图中时。我的点击部分正在工作,但滚动到顶部没有按我想要的那样工作。

tableview嵌入了一个大标题的navigationbarsearchbar嵌入了header,所以它会随着滚动展开和折叠(默认行为)。

我正在使用以下内容进行滚动,正如预期的那样,滚动到第一个 table 视图单元格,而不是展开导航栏 and/or 搜索栏:

[tv scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
          atScrollPosition:UITableViewScrollPositionTop animated:YES];

我似乎不知道如何滚动到顶部并展开导航栏和搜索栏。手动计算 tableview 的内容偏移量是行不通的,因为滚动 tableviewtableview 的偏移量明显不同。此外,我无法存储偏移量,因为不同的屏幕尺寸对于扩展 navigationbarsearchbar.

具有不同的内容偏移量

有没有人能解决这个问题?

好吧,我来晚了一点,但供以后参考:

对于 iOS 10+(足够了,因为在 11 中引入了增长的导航条)你可以使用

scrollView.perform(NSSelectorFromString("_scrollToTopIfPossible:"), with: true)

这是私人API。我已经搜索了几个小时并尝试了很多东西,但最后这是唯一有效的方法。请注意,WhatsApp 似乎也在使用相同的方法。也许他们与 Apple 有特殊交易或隐藏 api 调用 (cough base64 cough).

@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