【问题标题】:How to fix iOS 11 search bar jumping to top of screen on select (Swift)?如何修复 iOS 11 搜索栏在选择时跳到屏幕顶部(Swift)?
【发布时间】:2018-03-05 22:58:24
【问题描述】:

具体来说,在 iOS 11(而不是以前的操作系统)中,当您点击搜索栏开始输入时,它会跳到屏幕顶部。

当搜索栏已经靠近屏幕顶部时,我可以使用以下代码解决此问题:

if #available(iOS 11, *) {
     navigationItem.searchController = searchController

     navigationItem.hidesSearchBarWhenScrolling = false
} else {
     BugsTable.tableHeaderView = searchController.searchBar
}

但是,当我在视图中间有一个搜索栏时(即其他元素在其上方),该代码将无法工作,因为它告诉导航控制器内的搜索栏。我看到了以下问题,但对 Swift 没有有效的答案:

iOS 11 search bar jumping to top of screen

iOS 11 Searchcontroller jumps top of screen

我的 ViewDidLoad 设置参数是:

searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.sizeToFit()
ResultsTable.tableHeaderView = searchController.searchBar
searchController.searchBar.barTintColor = MyGlobalVariable.themeMainColor

我在这里缺少什么?

【问题讨论】:

    标签: iphone swift ios11 xcode9


    【解决方案1】:

    试试这个:

       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"];
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多