更改UISearchBar文本字段的borderStyle

时间:2013-03-17 21:36:19

标签: objective-c cocoa-touch uitextfield uisearchbar uiappearance

我正在寻找一种方法来获得具有不同背景和文字颜色的UISearchBar,并且没有边框。

我已经这样做了:

[[[self.searchBar subviews] objectAtIndex:0] setAlpha:0.0];
UITextField* searchField = nil;
for(int i = 0; i < self.searchBar.subviews.count; i++) {
    if([[self.searchBar.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) { //conform?
        searchField = [self.searchBar.subviews objectAtIndex:i];
    }
}
if(searchField) {
    searchField.backgroundColor = [UIColor colorWithRed:253.0/255.0 green:242.0/255.0 blue:210.0/255.0 alpha:1.0];
    searchField.textColor = [UIColor colorWithRed:98.0/255.0 green:65.0/255.0 blue:48.0/255.0 alpha:1.0];
    searchField.borderStyle = UITextBorderStyleNone;
}

但我正在寻找一种方法来更改borderStyle文本字段的UISearchBar。我还想删除放大镜按钮。这是修改这些修改的方法吗?

2 个答案:

答案 0 :(得分:1)

您可以使用外观API,而不是搜索搜索栏的视图层次结构。看看"Customizing Appearance" in the docs

答案 1 :(得分:0)

在Swift中,你可以使用它:

let txtField = UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self])
txtField.borderStyle = .None