温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何更改iOS上的UISearchBar组件的内部背景颜色

发布时间:2020-10-05 09:32:18 来源:网络 阅读:758 作者:卓行天下 栏目:移动开发

如何更改iOS上的UISearchBar组件的内部背景颜色

1. 使用此代码改变搜索栏“UITextField将backgroundImageUITextField *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 p_w_picpathNamed:@"yourImage"]];//just add here gray p_w_picpath which you display in quetion
[searchField setBorderStyle:UITextBorderStyleNone];
}

该波纹管代码的变化UISearchBarIcon UIImageView *searchIcon = [[UIImageView alloc] initWithImage:[UIImage p_w_picpathNamed:@"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 p_w_picpathNamed:@"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]];
}
}

 


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI