您好,我使用此代码来设置我的栏按钮的背景图像。我把它放在"AppDelegate.h“中
//-- set bar button image
UIImage *barButtonImage = [[UIImage imageNamed:@"bar-button"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 6, 0, 6)];
[[UIBarButtonItem appearance] setBackgroundImage:barButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];它工作得很好,但在我的搜索控制器中,我不想使用它,因为它使我的“搜索”按钮看起来非常丑陋。有没有办法只在搜索控制器中删除背景图像?请帮帮我。谢谢!

发布于 2013-11-25 15:30:21
在你的SearchController.m中尝试
[[UIBarButtonItem appearance] setBackgroundImage:nil forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];发布于 2013-11-25 15:31:57
[searchBarObject setImage:nil forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];这对我来说很有效..我希望它能对你有所帮助。
已更新完整代码..viewDidLoad中的.m文件内容
//Table initialization..........................................................................
my_table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height)];
//SearchBar initialization......................................................................
UISearchBar *search = [UISearchBar new];
[search sizeToFit];
search.delegate = self;
//Make the image in size 20x20................................................................
UIImage *img = [UIImage imageNamed:@"apple.jpg"];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(20, 20), YES, 0);
[img drawInRect:CGRectMake(0, 0, 20, 20)];
UIImage *img2 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//Set the image for the UISearchbarIconSearch...................................................
[search setImage:img2 forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
search.barStyle = UIBarStyleBlackTranslucent;
[my_table setTableHeaderView:search];
[my_table reloadData];
//SearchDisplay initialization..................................................................
UISearchDisplayController *display = [[UISearchDisplayController alloc]initWithSearchBar:search contentsController:self];
sbc = display; //
display.delegate = self;
display.searchResultsDataSource = self;
display.searchResultsDelegate = self;
my_table.dataSource = self;
my_table.delegate = self;
[self.view addSubview:my_table];.h文件。
@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate>
{
NSArray *table_array;
UITableView *my_table;
UISearchDisplayController *sbc;
NSArray *filtered;
}
@end这是我的代码,它改变了搜索图标的图像...
发布于 2013-11-25 15:56:16
如果您想要删除搜索控制器的背景图像
[[searchBar.subviews objectAtIndex:0] removeFromSuperview];你可以给它添加清晰的背景图片。
UIImageView *searchImage=[[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 319.0, 44.0)];
[searchImage setImage:[UIImage imageNamed:@"searchbg.png"]];
[searchBar insertSubview:searchImage atIndex:1];https://stackoverflow.com/questions/20186860
复制相似问题