在我的iPhone应用程序中,我正在尝试将背景图像设置为UISearchBar,我的图像未显示。
这是我的代码
UISearchBar * tempSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake( 0, 0, self.view.bounds.size.width, 40 )];
self.searchBar = tempSearchBar;
tempSearchBar.delegate = self;
self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.searchBar.barStyle = UIBarStyleBlack;
self.searchBar.backgroundColor=[UIColor clearColor];
self.searchBar.backgroundImage=[UIImage imageNamed:@"Searchbox.png"];
self.searchBar.placeholder = @"Search My Data";
[self.searchBar sizeToFit];
[self.view addSubview:self.searchBar];
[tempSearchBar release];
答案 0 :(得分:8)
[self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"Searchbox.png"] forState:UIControlStateNormal];
通过使用上述方法,我可以为searchBar设置bg Image
答案 1 :(得分:2)
[self.searchDisplayController.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"search_bar"] forState:UIControlStateNormal];
虽然Interface Builder提供了以下代码的等效功能(但不是所需的效果)
[self.searchDisplayController.searchBar setBackgroundImage:[UIImage imageNamed:@"search_bar"] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
答案 2 :(得分:0)
for(UIView * searchBarCity.subviews中的子视图){
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
{
[subview removeFromSuperview];
break;
}
}
for (UIView *subview in searchBarCity.subviews)
{
if ([subview conformsToProtocol:@protocol(UITextInputTraits)])
{
[(UITextField *)subview setClearButtonMode:UITextFieldViewModeNever];
}
}
for(UIView *subView in searchBarCity.subviews)
{
if([subView isKindOfClass: [UITextField class]])
{
[(UITextField *)subView setKeyboardAppearance: UIKeyboardAppearanceAlert];
}
}
UITextField *searchField = [searchBarCity valueForKey:@"_searchField"];
[searchField setBackground:[UIImage imageNamed:@"SearchInputBox.png"]];// = [UIColor clearColor];
searchField.borderStyle = UITextBorderStyleNone;
[searchField setTextColor:[UIColor blackColor]];
UIImageView* image =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
image.image = [UIImage imageNamed:@"navigationBarColorCode.png"];
image.userInteractionEnabled = FALSE;
[searchBarCity insertSubview:image belowSubview:searchField];
searchBarCity.userInteractionEnabled = YES;
searchBarCity.placeholder = @"Search";
}
答案 3 :(得分:0)
使用以下代码
[self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"search_box.png"] forState:UIControlStateNormal];
self.searchDisplayController.searchBar.barTintColor = [UIColor clearColor];
self.searchBar.tintColor=[UIColor orangeColor];
答案 4 :(得分:0)
设置SearchBar的背景图片:
[self.searchController.searchBar setBackgroundImage:[UIImage new] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
然后更改SearchField背景图片:
[self.searchController.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"Searchbox.png"] forState:UIControlStateNormal];
如果您不想在搜索栏中显示“搜索图标”,请执行以下操作:
UITextField *searchField = (UITextField *)[self.searchController.searchBar valueForKey:@"searchField"];
searchField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LEFT_VIEW_WIDTH, searchField.frame.size.height)];