溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何更改iOS上的UISearchBar組件的內(nèi)部背景顏色

發(fā)布時間:2020-10-05 09:32:18 來源:網(wǎng)絡(luò) 閱讀:764 作者:卓行天下 欄目:移動開發(fā)

如何更改iOS上的UISearchBar組件的內(nèi)部背景顏色

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圖標(biāo)搜索欄的你哪些可用的iOS 5 +- (void)setImage:(UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state

在這里你可以設(shè)置4種UISearchBarIcon即UISearchBarIconBookmarkUISearchBarIconClearUISearchBarIconResultsListUISearchBarIconSearch我希望這可以幫助您... 

2. 根據(jù)的UISearchBar 你這個函數(shù)適用于iOS 5.0 +。- (void)setSearchFieldBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state

用例:[mySearchBar setSearchFieldBackgroundImage:myImage forState:UIControlStateNormal];

可悲的是,在iOS 4的你需要恢復(fù)到較少見其他的答案。 

3. 只是自定義文本字段本身。 我只是這樣做的,它工作正常(的iOS 7)。UITextField *txfSearchField = [_searchBar valueForKey:@"_searchField"];
txfSearchField.backgroundColor = [UIColor redColor];

這樣,您就不需要創(chuàng)建一個圖像,它的大小,等.. 

4. 對于只改變顏色:searchBar.tintColor = [UIColor redColor];

為應(yīng)用背景圖像:[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問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI