如何更改iOS上的UISearchBar组件的内部背景颜色
2个回答
展开全部
修改UISearchBar背景颜色
UISearchBar是由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField. 要IB中没有直接操作背景的属性。方法一:是直接将 UISearchBarBackGround移去。方法二:是创建一个UIView设置其颜色加载到UISearchBarBaceGround上作为UISearchBar的背景颜色
方法一:
UISearchBar *seachBar=[[UISearchBar alloc] init];
//修改搜索框背景
seachBar.backgroundColor=[UIColor clearColor];
//去掉搜索框背景
//1.
[[searchbar.subviews objectAtIndex:0]removeFromSuperview];
//2.
for (UIView *subview in seachBar.subviews)
{
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
{
[subview removeFromSuperview];
break;
}
}
//3自定义背景
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"40-di.png"]];
[mySearchBar insertSubview:imageView atIndex:1];
UISearchBar是由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField. 要IB中没有直接操作背景的属性。方法一:是直接将 UISearchBarBackGround移去。方法二:是创建一个UIView设置其颜色加载到UISearchBarBaceGround上作为UISearchBar的背景颜色
方法一:
UISearchBar *seachBar=[[UISearchBar alloc] init];
//修改搜索框背景
seachBar.backgroundColor=[UIColor clearColor];
//去掉搜索框背景
//1.
[[searchbar.subviews objectAtIndex:0]removeFromSuperview];
//2.
for (UIView *subview in seachBar.subviews)
{
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
{
[subview removeFromSuperview];
break;
}
}
//3自定义背景
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"40-di.png"]];
[mySearchBar insertSubview:imageView atIndex:1];
ZESTRON
2024-09-04 广告
2024-09-04 广告
在Dr. O.K. Wack Chemie GmbH,我们高度重视ZESTRON的表界面分析技术。该技术通过深入研究材料表面与界面的性质,为提升产品质量与可靠性提供了有力支持。ZESTRON的表界面分析不仅涵盖了相变化、化学反应、吸附与解吸...
点击进入详情页
本回答由ZESTRON提供
2015-01-16 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517193
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
1. 使用此代码改变搜索栏“UITextField将backgroundImage
UITextField *searchField;
NSUInteger numViews = [searchBar.subviews count];
for(int i = 0; i < numViews; i++) {
if([[searchBar.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) { //conform?
searchField = [searchBar.subviews objectAtIndex:i];
}
}
if(!(searchField == nil)) {
searchField.textColor = [UIColor whiteColor];
[searchField setBackground: [UIImage imageNamed:@"yourImage"]];//just add here gray image which you display in quetion
[searchField setBorderStyle:UITextBorderStyleNone];
}
该波纹管代码的变化UISearchBarIcon
UIImageView *searchIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourSearchBarIconImage"]];
searchIcon.frame = CGRectMake(10, 10, 24, 24);
[searchBar addSubview:searchIcon];
[searchIcon release];
也为改变searcBar图标搜索栏的你哪些可用的iOS 5 +
- (void)setImage:(UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state
在这里你可以设置4种UISearchBarIcon即UISearchBarIconBookmarkUISearchBarIconClearUISearchBarIconResultsListUISearchBarIconSearch我希望这可以帮助您...
2. 根据的UISearchBar 你这个函数适用于iOS 5.0 +。
- (void)setSearchFieldBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state
用例:
[mySearchBar setSearchFieldBackgroundImage:myImage forState:UIControlStateNormal];
可悲的是,在iOS 4的你需要恢复到较少见其他的答案。
3. 只是自定义文本字段本身。 我只是这样做的,它工作正常(的iOS 7)。
UITextField *txfSearchField = [_searchBar valueForKey:@"_searchField"];
txfSearchField.backgroundColor = [UIColor redColor];
这样,您就不需要创建一个图像,它的大小,等..
4. 对于只改变颜色:
searchBar.tintColor = [UIColor redColor];
为应用背景图像:
[self.searchBar setSearchFieldBackgroundImage:
[UIImage imageNamed:@"Searchbox.png"]
forState:UIControlStateNormal];
5. 私有API的:
for (UIView* subview in [[self.searchBar.subviews lastObject] subviews]) {
if ([subview isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField*)subview;
[textField setBackgroundColor:[UIColor redColor]];
}
}
UITextField *searchField;
NSUInteger numViews = [searchBar.subviews count];
for(int i = 0; i < numViews; i++) {
if([[searchBar.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) { //conform?
searchField = [searchBar.subviews objectAtIndex:i];
}
}
if(!(searchField == nil)) {
searchField.textColor = [UIColor whiteColor];
[searchField setBackground: [UIImage imageNamed:@"yourImage"]];//just add here gray image which you display in quetion
[searchField setBorderStyle:UITextBorderStyleNone];
}
该波纹管代码的变化UISearchBarIcon
UIImageView *searchIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yourSearchBarIconImage"]];
searchIcon.frame = CGRectMake(10, 10, 24, 24);
[searchBar addSubview:searchIcon];
[searchIcon release];
也为改变searcBar图标搜索栏的你哪些可用的iOS 5 +
- (void)setImage:(UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state
在这里你可以设置4种UISearchBarIcon即UISearchBarIconBookmarkUISearchBarIconClearUISearchBarIconResultsListUISearchBarIconSearch我希望这可以帮助您...
2. 根据的UISearchBar 你这个函数适用于iOS 5.0 +。
- (void)setSearchFieldBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state
用例:
[mySearchBar setSearchFieldBackgroundImage:myImage forState:UIControlStateNormal];
可悲的是,在iOS 4的你需要恢复到较少见其他的答案。
3. 只是自定义文本字段本身。 我只是这样做的,它工作正常(的iOS 7)。
UITextField *txfSearchField = [_searchBar valueForKey:@"_searchField"];
txfSearchField.backgroundColor = [UIColor redColor];
这样,您就不需要创建一个图像,它的大小,等..
4. 对于只改变颜色:
searchBar.tintColor = [UIColor redColor];
为应用背景图像:
[self.searchBar setSearchFieldBackgroundImage:
[UIImage imageNamed:@"Searchbox.png"]
forState:UIControlStateNormal];
5. 私有API的:
for (UIView* subview in [[self.searchBar.subviews lastObject] subviews]) {
if ([subview isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField*)subview;
[textField setBackgroundColor:[UIColor redColor]];
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询