SearchDisplayController SearchBar overlapping navigation bar and resizing itself IOS7

a 夏天 提交于 2019-12-07 04:48:33

问题


My app has a tableview controller that is presented as a form sheet.

On top portion of tableviewcontoller there is a UView and inside of that UIView there is a navigation bar and a searchbar.

Everything works fine older version of IOS but in IOS7 when user taps searchbar everything is messes up.

Normal:

When User starts to type:

After seach ends:

in.h

UITableViewController<UITextFieldDelegate,UISearchDisplayDelegate,UISearchBarDelegate>
@property (nonatomic,weak) IBOutlet UINavigationBar *topBar;

Tried few things but code doesnt seem to be changing anything, when put a breakpoint it enters to delegate methods though

in.m

//-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
//    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
//        CGRect statusBarFrame =  self.topBar.frame;
//        [UIView animateWithDuration:0.25 animations:^{
//            for (UIView *subview in self.tableView.subviews)
//                subview.transform = CGAffineTransformMakeTranslation(0, statusBarFrame.size.height+50);
//        }];
//    }
//}
//
//-(void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
//    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
//        [UIView animateWithDuration:0.25 animations:^{
//            for (UIView *subview in self.tableView.subviews)
//                subview.transform = CGAffineTransformIdentity;
//        }];
//    }
//}

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        CGRect frame = self.searchDisplayController.searchBar.frame;
        frame.origin.y += self.topBar.frame.size.height;
         self.searchDisplayController.searchBar.frame = frame;
    }
}

- (void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        CGRect statusBarFrame = self.topBar.frame;
        CGRect frame =  self.searchDisplayController.searchBar.frame;
        frame.origin.y -= statusBarFrame.size.height;
         self.searchDisplayController.searchBar.frame= frame;
    }
}


#Additional Info
- (void)viewDidLoad
{
    [super viewDidLoad];
      if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
    {
        //self.searchDisplayController.searchBar.searchBarStyle= UISearchBarStyleProminent;
        self.edgesForExtendedLayout = UIRectEdgeNone;
        //self.edgesForExtendedLayout = UIRectEdgeLeft | UIRectEdgeBottom | UIRectEdgeRight;
    }
}

According to break points frame position and sizes are correct but they dont change self.searchDisplayController.searchBar.frame at all

I have also tried

-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        [self.topBar setHidden:YES];

    }
}

-(void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        [self.searchDisplayController.searchBar removeFromSuperview];
        CGRect frame = self.searchDisplayController.searchBar.frame;
        frame.origin.y += self.topBar.frame.size.height;
        self.searchDisplayController.searchBar.frame = frame;
        [self.topView addSubview:self.searchDisplayController.searchBar];
        [self.topView bringSubviewToFront:self.topBar];

        [self.topBar setHidden:NO];
    }
}

How can I solve this issue ?


回答1:


try setting this value in your table view controller:

if ([self respondsToSelector:@selector(edgesForExtendedLayout)]){
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

And change you view hierarchy to have a view, and tableView, search bar and nag bar as it's subviews. Make your table view controller a view controller and make it as the data source and delegate.

Or don't use a searchBarDisplayController and just use a search bar with it's delegate methods:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText



回答2:


I had a similar problem, I think searchDisplayController expands searchBar size to full tableHeaderView size (I really don't know why it doing this). I had searchbar and one custom toolbar (both 44px height) in tableHeaderView.

I've workarounded it with:

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
    self.tableView.tableHeaderView.frame = CGRectMake(0, 0, self.tableView.bounds.size.width, 44);
}

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
    self.tableView.tableHeaderView.frame = CGRectMake(0, 0, self.tableView.bounds.size.width, 88);
}

So I just setting tableHeaderView to the size of single UISearchBar when entering search and set it back when all animations complete. This solved my problem. Even animations still work (but they does not in the childViewController. No idea why)



来源:https://stackoverflow.com/questions/19818998/searchdisplaycontroller-searchbar-overlapping-navigation-bar-and-resizing-itself

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!