2017-09-15 88 views
2

我使用UISearchController作为导航栏的一部分使用的iOS推出了新的API 11.我在我的ViewController的viewDidLoad错误放置11

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [_table setDataSource:self]; 
    [_table setDelegate:self]; 
    searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; 
    [self.navigationItem setSearchController:searchController]; 
    [self.navigationItem setHidesSearchBarWhenScrolling:NO]; 
    [searchController.searchBar setBackgroundColor:[UIColor greenColor]]; 
} 
以下述方式使用它

但是,搜索文本字段呈现在搜索栏内的错误位置。看看下面的截图。

https://imgur.com/a/Igf49

我检查了视图层次,发现UISearchBarTextField对象(这是不能直接访问开发者),在搜索栏中有一个1 frame.y值这可能是导致此问题。我已经在iOS 11 beta 10和iOS 11 GM上测试过了。

这是一个已知的问题?有没有解决这个问题?或者是我在我身上做错了什么?

任何帮助将不胜感激(:

+0

嗨,检查我的回答如下,请。希望能帮助到你! –

回答

2

您的图像(确切的问题)是无法访问的所以可以看到确切的问题,但是从你的说法,我找到了你的问题,我试着以下为iOS 11和它的工作。罚款。

我的代码有一个解决问题的方法,但它是在斯威夫特,你需要将其转换成Objective-C的,这不会是你很难做到这一点。

if #available(iOS 11.0, *) { 
       let sc = UISearchController(searchResultsController: nil) 
       sc.delegate = self 
       let scb = sc.searchBar 
       scb.tintColor = UIColor.white 
       scb.barTintColor = UIColor.white 


       if let textfield = scb.value(forKey: "searchField") as? UITextField { 
        textfield.textColor = UIColor.blue 
        if let backgroundview = textfield.subviews.first { 

         // Background color 
         backgroundview.backgroundColor = UIColor.white 

         // Rounded corner 
         backgroundview.layer.cornerRadius = 5; 
         backgroundview.clipsToBounds = true; 

        } 
       } 

       if let navigationbar = self.navigationController?.navigationBar { 
        navigationbar.barTintColor = UIColor.blue 
       } 
       navigationItem.searchController = sc 
       navigationItem.hidesSearchBarWhenScrolling = false 

    } 

输出:

enter image description here

3

这里的代码演示了如何改变文本框的背景颜色在搜索栏上的iOS 11.

前: Default state of iOS 11 searchBar

后: enter image description here

的OBJ-C:在(无效)viewDidLoad中使用此代码:

if (@available(iOS 11, *)) { 
    UITextField *textField = [self.searchController.searchBar valueForKey:@"searchField"]; 
    UIView *backgroundView = textField.subviews.firstObject; 
    backgroundView.backgroundColor = UIColor.whiteColor; 
    backgroundView.layer.cornerRadius = 10; 
    backgroundView.clipsToBounds = YES; 
} 

斯威夫特:在viewDidLoad中()使用此代码:

if #available(iOS 11.0, *) { 
    if let textfield = scb.value(forKey: "searchField") as? UITextField { 
     textfield.textColor = UIColor.blue 

     if let backgroundview = textfield.subviews.first { 
      backgroundview.backgroundColor = UIColor.white 
      backgroundview.layer.cornerRadius = 10; 
      backgroundview.clipsToBounds = true; 
     } 
    } 
} 
+1

我一直在寻找这个答案几个小时。 +10000 –