0

我想删除默认搜索栏的边框并实现普通搜索栏。见下图

默认搜索栏,

默认搜索栏

这就是我想要达到的目标,

普通搜索栏

我用谷歌搜索并尝试了下面的代码,但没有达到预期的效果,

for (id img in searchbar.subviews) 
{
    if ([img isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) 
    {
        [img removeFromSuperview];
    }
}

searchbar.delegate = self;
searchbar.layer.borderWidth = 0;
searchbar.layer.borderColor = [[UIColor clearColor] CGColor];

我该如何实施?

4

2 回答 2

1

感谢您的回答。我已经使用下面的代码解决了我的问题....

以下代码中使用的纯白色搜索栏背景图像

UITextField *txfSearchField = [self.searchbar valueForKey:@"_searchField"];
        [txfSearchField setBackgroundColor:[UIColor whiteColor]];
        [txfSearchField setLeftViewMode:UITextFieldViewModeNever];
        [txfSearchField setRightViewMode:UITextFieldViewModeNever];
        [txfSearchField setBackground:[UIImage imageNamed:@"searchbar_bgImg.png"]];
        [txfSearchField setBorderStyle:UITextBorderStyleNone];
        //txfSearchField.layer.borderWidth = 8.0f;
        //txfSearchField.layer.cornerRadius = 10.0f;
        txfSearchField.layer.borderColor = [UIColor clearColor].CGColor;
        txfSearchField.clearButtonMode=UITextFieldViewModeNever;

        [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:@"TimesNewRomanPS-BoldItalicMT" size:20]];
        [[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor blackColor]];
于 2013-10-21T15:05:56.147 回答
0

在 iOS 7 中有一个小的变化,现在你必须迭代两个级别。

 for (UIView *subView in self.searchBar.subViews){
    for (UIView *2ndLeveSubView in subView.subViews){
    if ([2ndLevelSubView isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
        {
            [2ndLevelSubView removeFromSuperView];
        }
    }
   }
于 2013-10-15T08:10:57.053 回答