我有自定义搜索栏。每当我在iOS 8
中启动时,它都无法正常运行。我也发布了代码。
CGRect searchBarFrame = CGRectMake(-10.0, 0.0,
self.navigationController.navigationBar.frame.size.width+20,
self.navigationController.navigationBar.frame.size.height);
if(kEnableScanFunctionality)
{
//searchBarFrame.origin.x += 60; //60 is Left BarButtonItem width
searchBarFrame.size.width = searchBarFrame.size.width - 60.0f;
}
searchBar = [[UISearchBar alloc] initWithFrame:searchBarFrame];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
searchBar.backgroundImage = [self setSeachBarBackgroudImage];
searchBar.placeholder = NSLocalizedString(@"SearchBar_Placeholder",@"Search");
//This code is used to remove 1 pixel height imageView
if ([[UIDevice currentDevice].systemVersion floatValue] < 7.0f)
{
[[searchBar.subviews objectAtIndex:0] removeFromSuperview];
}
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
searchBarView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[searchBarView setBackgroundColor:[UIColor clearColor]];
searchBar.delegate = self;
//custom cancel button
NSDictionary *attributes = nil;
UIBarButtonItem *cancelBtn = [UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil];
UIImage *cancelImage = nil;
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)
{
cancelImage = [UIImage imageNamed:@"cancelButton.png"];
attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor,[UIFont fontWithName:@"HelveticaNeue" size:14.0f], UITextAttributeFont,nil];
}
else
{
cancelImage = [[UIImage imageNamed:@"cancelButton.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,[UIFont fontWithName:@"HelveticaNeue" size:14.0f], NSFontAttributeName,nil];
}
[cancelBtn setBackgroundImage:cancelImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[cancelBtn setTitleTextAttributes:attributes forState:UIControlStateNormal];
[cancelBtn setTitleTextAttributes:attributes forState:UIControlStateHighlighted];
//[cancelBtn setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor], UITextAttributeTextColor,nil] forState:UIControlStateHighlighted];
[searchBarView addSubview:searchBar];
self.navigationItem.titleView = searchBarView;
if(kEnableScanFunctionality)
{
//searchBarFrame.origin.x += 60; //60 is Left BarButtonItem width
searchBarFrame.size.width = searchBarFrame.size.width - 60.0f;
}
searchBar = [[UISearchBar alloc] initWithFrame:searchBarFrame];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
searchBar.backgroundImage = [self setSeachBarBackgroudImage];
searchBar.placeholder = NSLocalizedString(@"SearchBar_Placeholder",@"Search");
//This code is used to remove 1 pixel height imageView
if ([[UIDevice currentDevice].systemVersion floatValue] < 7.0f)
{
[[searchBar.subviews objectAtIndex:0] removeFromSuperview];
}
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height)];
searchBarView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[searchBarView setBackgroundColor:[UIColor clearColor]];
searchBar.delegate = self;
//custom cancel button
NSDictionary *attributes = nil;
UIBarButtonItem *cancelBtn = [UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil];
UIImage *cancelImage = nil;
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)
{
cancelImage = [UIImage imageNamed:@"cancelButton.png"];
attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor,[UIFont fontWithName:@"HelveticaNeue" size:14.0f], UITextAttributeFont,nil];
}
else
{
cancelImage = [[UIImage imageNamed:@"cancelButton.png"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal];
attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,[UIFont fontWithName:@"HelveticaNeue" size:14.0f], NSFontAttributeName,nil];
}
[cancelBtn setBackgroundImage:cancelImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[cancelBtn setTitleTextAttributes:attributes forState:UIControlStateNormal];
[cancelBtn setTitleTextAttributes:attributes forState:UIControlStateHighlighted];
//[cancelBtn setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor], UITextAttributeTextColor,nil] forState:UIControlStateHighlighted];
[searchBarView addSubview:searchBar];
self.navigationItem.titleView = searchBarView;
如何克服这个问题?