UISearchController wrong animation on swipe back then disappearing

时间:2019-01-07 13:10:25

标签: ios iphone xcode navigationbar searchbar

To make this short, I have a main view controller that has the navigation bar hidden, from that VC's navigation controller, I push another view controller which has the navigation bar visible. Then I add a searchController to the navigationItem to add the searchBar when I scroll down. Everything is working fine until you swipe back to pop the VC.

The navigation bar will animate leave the screen with the VC, but the searchBar will animate as if it's going up in place. What's worse is if I cancel the swipe to pop, the whole searchBar disappears and a black view appears instead (I think the searchController background ow whatever view background).

I tried everything, it's a few lines of code yet I've been pulling my hair to solve this. I know I can just disable swipe to pop but I don't want that(+ even when pressing back the searchBar animates in the same weird way) and I don't want to add a searchBar in any other way.

In the attached video I'm just swiping with my finger to show the animation.

desc

- (void)viewDidLoad {
    [super viewDidLoad];
    self.definesPresentationContext = YES;
    [self.navigationController setNavigationBarHidden:NO];

    if (@available(iOS 11.0, *)) {
        self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
        _searchController.delegate = self;
        _searchController.searchResultsUpdater = self;
        _searchController.searchBar.placeholder = @"Search";
        self.navigationItem.searchController = _searchController;
        self.navigationItem.hidesSearchBarWhenScrolling = YES;
    }
}

5 个答案:

答案 0 :(得分:0)

我认为添加额外的UISearchController实在是过大了。您始终可以通过情节提要或代码将独立的UISearchbar添加到视图控制器,并使您的搜索栏托管视图控制器成为该搜索栏的委托。

类似这样的东西:

    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 0, NAVBAR_HEIGHT)];   
    self.searchBar.delegate = self;
    self.view.addSubView(self.searchBar);

这样,与搜索栏结合的导航项将消失,希望该问题得以解决。

答案 1 :(得分:0)

将搜索栏放置在导航项目的标题视图中,而不是将var allDupes = recs.SelectMany(r => r.REC_Clusters) .Select(r => r.GroupBy(c => c) .Where(g => g.Count() > 1) .SelectMany(g => g)) .Where(r => r.Any()).ToArray(); 分配给UISearchController searchController

替换

UINavigationItem's

navigationItem.searchController

更新后的答案:

该问题归因于用于隐藏navigationItem.titleView = searchController?.searchBar 的方法 隐藏NavigationBar

的方法有多种
NavigationBar

似乎您使用了setter only属性隐藏在self.navigationController?.setNavigationBarHidden(false, animated: true) self.navigationController?.navigationBar.isHidden = false self.navigationController?.isNavigationBarHidden = false

MasterViewController

相反,使用self.navigationController?.setNavigationBarHidden(true, animated: true) 既是设置器又是获取器。

isNavigationBarHidden

还要从override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.navigationController?.isNavigationBarHidden = true } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) self.navigationController?.isNavigationBarHidden = false } 中删除setNavigationBarHidden

干杯!

答案 2 :(得分:0)

我在这里尝试了答案,但是没有一个没有按我想要的那样工作。以下解决方案对我有用。

对于错误的动画,我在viewController中使用searchBar进行了以下操作:

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    let containerView = transitionCoordinator?.containerView
    let currentPosition = self.navigationItem.searchController?.searchBar.superview?.frame.origin.x
    transitionCoordinator?.animateAlongsideTransition(in: containerView, animation: { (context) in
        if context.viewController(forKey: .to) is PreviousViewController {
            self.navigationItem.searchController?.searchBar.superview?.frame.origin.x = self.view.frame.size.width

        }

    }, completion: { (context) in
        self.navigationItem.searchController?.searchBar.superview?.frame.origin.x = currentPosition ?? 0
    })
}

为使searchBar消失,我滚动了tableView,使searchBar出现了回来。问题在于searchBar已完全从其superView中删除。但是有了这个微小的滚动“动画”,搜索栏又回来了。虽然不是很酷,但确实有效:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    //workaround for showing the searchbar again if popgesture was canceled
    var offset = self.tableView.contentOffset
    offset.y -= 0.5
    UIView.animate(withDuration: 0.05, animations: {
        self.tableView.setContentOffset(offset, animated: false)
    }, completion: {_ in
        offset.y += 0.5
        self.tableView.setContentOffset(offset, animated: false)
    })
}

答案 3 :(得分:0)

我认为这是iOS 11、12错误,因为它可以在iOS 13 beta 7中正常运行。

答案 4 :(得分:0)

我已经尝试了很多方法来解决此问题,最后我找到了该解决方案,

尝试使用它:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    navigationItem.hidesSearchBarWhenScrolling = false
}