我想自定义搜索栏,我的意思是白盒子。我可以做吗? 有没有关于这方面的一些文件? 有没有办法隐藏白盒,但不是字母。 我可以至少让箱子变小吗?我的意思是少高度如何自定义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