美文网首页
UISearchBar 修改背景和textField

UISearchBar 修改背景和textField

作者: 蛮荒星域 | 来源:发表于2016-10-24 18:58 被阅读90次

    UISearchBar 修改背景和textField

    修改背景

    1.修改背景

    方法一:

    for (UIView *subview in [[yourSearchBar.subviews lastObject] subviews]) {
    if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
    [subview removeFromSuperview];
    break;
    }
    }

    方法二:

    for (UIView *subview in [[self.searchBar.subviews lastObject] subviews]) {
    if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
    [subview setAlpha:0.0];
    break;
    }
    }

    2.设置背景图片

    [searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"按钮"] forState:UIControlStateNormal];
    [searchBar setBackgroundImage:[UIImage imageNamed:@"按钮2"]];
    [searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"按钮1"] forState:UIControlStateHighlighted];

    修改textField

    1.获取

    UITextField *textField = [searchBar valueForKey:@"_searchField"];
    UITextField *searchField = [self.searchBar valueForKey:@"searchField"];
    for (UIView *subview in [[yourSearchBar.subviews lastObject] subviews]) {
    if ([subview isKindOfClass:[UITextField class]]) {
    //此处获取
    break;
    }
    }`

    2.修改搜索时的放大镜图标

    [searchBar setBackgroundImage:[[UIImage alloc] init] ];//if you want to remove background of uisearchbar
    UIImage *image = [UIImage imageNamed: @"00b0f0”]; //00b0f0是自己设置的图标
    [searchBar setImage:image forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];

    结构图

    相关文章

      网友评论

          本文标题:UISearchBar 修改背景和textField

          本文链接:https://www.haomeiwen.com/subject/owrvyttx.html