您好,登錄后才能下訂單哦!
如何更改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]];
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。