iOS 11 search bar jumping to top of screen

前端 未结 5 1987
青春惊慌失措
青春惊慌失措 2020-12-09 19:07

I\'ve got a UITableView in which I set its header to be a search bar.

tableView.tableHeaderView = searchController.searchBar

Everything wor

5条回答
  •  悲哀的现实
    2020-12-09 19:22

    Try this:

    if (@available(iOS 11, *)){
        [searchVC.searchBar addObserver:self forKeyPath:@"frame" options: NSKeyValueObservingOptionNew context:nil];
    }else{
        [view addSubview:searchVC.searchBar];
    }
    
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
        CGRect rect = [change[@"new"] CGRectValue];
        if (rect.origin.y == 14) {
            CGRect temp = rect;
            temp.origin.y = 20;
            [self.searchVC.searchBar setValue:@(temp) forKey:@"frame"];
        }else if (rect.size.height == 56){
            CGRect temp = rect;
            temp.size.height = 50;
            [self.searchVC.searchBar setValue:@(temp) forKey:@"frame"];
        }
    }
    

提交回复
热议问题