您的位置:首页 > 产品设计 > UI/UE

UISearchbar设置相关总结

2016-04-06 18:56 453 查看

去掉输入框后面的背景框

突然有一天发现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 = [_searchBar
valueForKey:@"_searchField"];

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

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

searchTextField.textColor = [UIColor
whiteColor];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: