0

在我的 iPhone 应用程序中,我试图将背景图像设置为 UISearch Bar,但我的图像没有显示出来。

这是我的代码

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];
4

5 回答 5

8
[self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"Searchbox.png"] forState:UIControlStateNormal];

通过使用上述方法,我可以为 searchBar 设置 bg Image

于 2013-03-04T05:56:55.303 回答
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];
于 2015-01-22T07:40:50.213 回答
0
  • (void) clearSearchBarBg {

for (UIView *subview in 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";

}

于 2014-06-25T06:45:38.507 回答
0

使用下面的代码

[self.searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"search_box.png"] forState:UIControlStateNormal]; 
self.searchDisplayController.searchBar.barTintColor = [UIColor clearColor];
self.searchBar.tintColor=[UIColor orangeColor];
于 2014-12-19T06:20:14.913 回答
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)];
于 2018-10-24T09:49:04.437 回答