iOS 11中搜索文本框放置位置错误,应放在搜索栏内部

3

我正在使用iOS 11中引入的新API,将UISearchController作为导航栏的一部分使用。在我的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上进行了测试。
这是一个已知的问题吗?是否有任何修复方法?还是我在我的端上做错了什么?
任何帮助将不胜感激(:)

嗨,请检查我的答案。希望能有所帮助! - Adam Studenic
2个回答

12

这里是一段代码,展示了如何在iOS 11上更改searchBar中textField的背景颜色。

修改前: iOS 11 searchBar默认状态

修改后: 输入图像描述

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

3
我已经寻找这个答案数小时了。+10000 - sanch

4

您的图像(确切的问题)无法访问,因此无法看到确切的问题。但从您的陈述中,我找到了您的问题。我在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

    }

输出:

在此输入图片描述


这段内容涉及IT技术,展示了一个图片的输出结果。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接