搜索栏在iOS 8中越界。但是它在iOS 7中运行良好

时间:2014-11-18 07:45:34

标签: ios objective-c iphone ipad ios8

我有自定义搜索栏。每当我在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;

如何克服这个问题?

0 个答案:

没有答案