我想自定義搜索欄,我的意思是白盒子。我可以做嗎? 有沒有關於這方面的一些文件? 有沒有辦法隱藏白盒,但不是字母。 我可以至少讓箱子變小嗎?我的意思是少高度如何自定義UISearchBar的外觀
38
A
回答
56
通過IOS 5.0,你必須使用
[[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:@"searchbar.png"]forState:UIControlStateNormal];
7
29
這裏的背景是解決方案,我一直在尋找: 子類一個UISearchBar,&覆蓋方法layoutSubviews
- (void)layoutSubviews {
UITextField *searchField;
NSUInteger numViews = [self.subviews count];
for(int i = 0; i < numViews; i++) {
if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) { //conform?
searchField = [self.subviews objectAtIndex:i];
}
}
if(!(searchField == nil)) {
searchField.textColor = [UIColor whiteColor];
[searchField setBackground: [UIImage imageNamed:@"buscador.png"] ];
[searchField setBorderStyle:UITextBorderStyleNone];
}
[super layoutSubviews];
}
+0
@Jhaliya,我應該在哪裏放的這部分代碼以獲取搜索欄自定義背景? – chatur 2012-01-19 09:57:29
1
有很少數企圖在那裏,包括子類的UISearchBar和破解它的layoutSubviews
或drawLayer:
的機會。 iOS 5之後,最好的方法是使用UIAppearance。
// Configure your images
UIImage *backgroundImage = [UIImage imageNamed:@"searchbar"];
UIImage *searchFieldImage = [[UIImage imageNamed:@"searchfield"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];
// Set it to your UISearchBar appearance
[[UISearchBar appearance] setBackgroundImage:backgroundImage];
[[UISearchBar appearance] setSearchFieldBackgroundImage:searchFieldImage forState:UIControlStateNormal];
1
使用下面的代碼,透明的搜索欄
// Set it to your UISearchBar appearance
[[UISearchBar appearance] setBackgroundColor:[UIColor clearColor]];
[[UISearchBar appearance] setBackgroundImage:backgroundImage];
[[UISearchBar appearance] setSearchFieldBackgroundImage:searchFieldImage
forState:UIControlStateNormal];
14
// Search Bar Customization
// Background Image
[self.searchDisplayController.searchBar setBackgroundImage:[[UIImage alloc]init]];
// Backgroud Color
[self.searchDisplayController.searchBar setBackgroundColor:[UIColor redColor]];
// Search Bar Cancel Button Color
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor whiteColor]];
// set Search Bar Search icon
[self.searchDisplayController.searchBar setImage:[UIImage imageNamed:@"search_ico.png"]
forSearchBarIcon:UISearchBarIconSearch
state:UIControlStateNormal];
// set Search Bar textfield background image
[[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:@"search_box.png"]
forState:UIControlStateNormal];
// set Search Bar texfield corder radius
UITextField *txfSearchField = [self.searchDisplayController.searchBar valueForKey:@"_searchField"];
txfSearchField.layer.cornerRadius = 10.8f;
8
0
一些例子Swift
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).defaultTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).attributedPlaceholder = NSAttributedString(string: "", attributes: [NSForegroundColorAttributeName : UIColor.whiteColor()])
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).tintColor = UIColor.whiteColor()
UISearchBar.appearance().setImage(UIImage(named: "searchBarSearchIcon"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal)
UISearchBar.appearance().setImage(UIImage(), forSearchBarIcon: UISearchBarIcon.Clear, state: UIControlState.Normal)
相關問題
- 1. 如何自定義asp.net DropDownList的外觀?
- 2. 如何自定義Apple App Store外觀?
- 3. 自定義`sliderInput()`外觀
- 4. 自定義UISlider外觀
- 5. 自定義控件外觀
- 6. 自定義textfieldUI外觀
- 7. 在AppDelegate中自定義UISearchBar&UINavigationBar的外觀?爲什麼要在課堂而不是實例級別自定義?
- 8. Android自定義FragmentPagerAdapter的外觀
- 9. iPhone UINavigationBar的自定義外觀
- 10. 自定義複選框的外觀starStyle
- 11. 自定義tabView標頭的外觀
- 12. 自定義emacs markdown-mode的外觀
- 13. UIPickerView的自定義外觀和感覺;
- 14. Drupal:自定義WorkFlow模塊的外觀
- 15. 自定義選擇選項的外觀
- 16. 如何在自定義列表視圖樣式中自定義GridViewHeaderRowPresenter的外觀?
- 17. UISearchDisplayController和自定義UISearchBar
- 18. 自定義片段選項卡主機外觀/外觀
- 19. 如何自定義Win32中的選項卡控件的外觀?
- 20. 自定義Swing外觀和自定義組件?
- 21. 自定義評論表單外觀
- 22. 自定義外觀plotly.js傳奇
- 23. Android - 自定義微調控件外觀
- 24. JTabbedPane自定義標籤外觀
- 25. 如何自定義WordPress中的Subscribe2按鈕外觀?
- 26. 如何自定義UITableview的索引外觀?
- 27. 如何自定義標籤欄的外觀?
- 28. 如何自定義SDK中Facebook分享按鈕的外觀?
- 29. 如何在Laravel 4中創建自定義的外觀
- 30. 如何自定義表視圖多選的外觀?
我們可以改變它的高度嗎? – 2013-01-01 12:31:59
指定一個背景圖像與你想要的高度,它應該改變它 – 2013-01-01 13:14:04
謝謝@Ilker Baltaci,已經試過它保持在原來的高度 – 2013-01-02 12:26:10