【问题标题】:Wrong placement of search text field inside search bar on iOS 11iOS 11 搜索栏中的搜索文本字段位置错误
【发布时间】:2017-09-15 14:10:49
【问题描述】:

我使用 UISearchController 作为导航栏的一部分,使用 iOS 11 中引入的新 API。我在 ViewController 的 viewDidLoad 中以下列方式使用它

- (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 对象(开发人员无法直接访问)的 frame.y 值为 1,这可能是导致此问题的原因。我已经在 iOS 11 beta 10 和 iOS 11 GM 上对此进行了测试。

这是一个已知问题吗?有什么解决办法吗?还是我做错了什么?

任何帮助将不胜感激(:

【问题讨论】:

  • 嗨,请在下面查看我的答案。希望对您有所帮助!

标签: ios objective-c uinavigationitem uisearchcontroller ios11


【解决方案1】:

这里的代码显示了如何在 iOS 11 上更改 searchBar 中 textField 的背景颜色。

之前:

之后:

Obj-C: 在 (void)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;
}

Swift: 在 vi​​ewDidLoad() 中使用以下代码:

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

【讨论】:

  • 我一直在寻找这个答案几个小时。 +10000
【解决方案2】:

您的图片(确切的问题)无法访问。所以可以看到确切的问题。但是从你的陈述中,我发现了你的问题。我尝试关注 iOS 11 并且运行良好。

我的代码可以解决您的问题,但它是用 Swift 编写的。您需要将其转换为 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

    }

输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-19
    • 2012-01-03
    • 2018-03-12
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    • 2011-06-18
    相关资源
    最近更新 更多