UISearchbar设置相关总结

来源:互联网 发布:fenlog软件破解版 编辑:程序博客网 时间:2024/06/14 04:07

去掉输入框后面的背景框

突然有一天发现searcher.backgroundColor = [UIColor clearColor];不管用了,那么,,,,又是一顿在网上找,,

os系统升级到7.1后,原来在7.0下显示正常的UISearchbar现在又出现问题了。究其原因,是由于UISearchbar的subview又做修改了。 

float version = [[[UIDevice currentDevice ] systemVersion ]floatValue ];

if ([ mySearchBar respondsToSelector : @selector (barTintColor)]) {

    float  iosversion7_1 = 7.1 ;

    if (version >= iosversion7_1)

    {

        //iOS7.1

        [[[[ mySearchBar . subviews objectAtIndex : 0 ] subviews ] objectAtIndex :0 ] removeFromSuperview ];

        [ mySearchBar setBackgroundColor :[ UIColor clearColor ]];

    }

    else

    {

        //iOS7.0

        [ mySearchBar setBarTintColor :[ UIColor clearColor ]];

        [ mySearchBar setBackgroundColor :[ UIColor clearColor ]];

    }

}

else

{

    //iOS7.0 以下

    [[ mySearchBar . subviews objectAtIndex : 0 ] removeFromSuperview ];

    [ mySearchBar setBackgroundColor :[ UIColor clearColor ]];

}



修改searchBar的输入框背景色

7.0以前:

for (UIView *subView in _searchBar.subviews) {

     if ([subView isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {

        subView.backgroundColor = RGBColor(19,115, 209);

     }

}


7.0及以后:

// 经测试, 需要设置barTintColor,才能拿到UISearchBarTextField对象

_searchBar.barTintColor =RGBColor(23,130, 235);

[[_searchBar.subviews[0]subviews] lastObject].backgroundColor =RGBColor(19,115, 209);


修改输入框的placeholder颜色和输入颜色

//获取searchBar里面的输入框有两种方法

//    UITextField *searchTextField = [[_searchBar.subviews[0] subviews] lastObject];

    UITextField *searchTextField = [_searchBarvalueForKey:@"_searchField"];

    searchTextField.backgroundColor = RGBColor(19, 115, 209);

    [searchTextField setValue:[UIColorwhiteColor]forKeyPath:@"_placeholderLabel.textColor"];

    searchTextField.textColor = [UIColorwhiteColor];


0 0
原创粉丝点击