SearchDisplayController SearchBar重叠导航栏并调整自身的大小IOS7

时间:2013-11-06 17:42:49

标签: ios objective-c ios7 uitableview uisearchbar

我的应用程序有一个tableview控制器,显示为表单。

在tableviewcontoller的顶部有一个UView,在UIView里面有一个导航栏和一个搜索栏。

一切都运行良好的旧版IOS,但在用户点击搜索栏的IOS7中,一切都很糟糕。

正常: enter image description here

当用户开始输入时: enter image description here

搜索结束后: enter image description here

in.h中

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

尝试了一些事情,但代码似乎没有改变任何东西,当它放入一个断点,它进入委托方法但

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;
    }
}

enter image description here

根据断点,帧位置和大小是正确的,但它们不会改变self.searchDisplayController.searchBar.frame at all

我也试过

-(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];
    }
}

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

尝试在表格视图控制器中设置此值:

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

并将视图层次结构更改为具有视图,并将tableView,搜索栏和nag栏更改为子视图。使表视图控制器成为视图控制器,并将其作为数据源和委托。

或者不要使用searchBarDisplayController,只使用带有委托方法的搜索栏:

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

答案 1 :(得分:2)

我有类似的问题,我认为searchDisplayController将searchBar大小扩展为full tableHeaderView大小(我真的不知道为什么这样做)。我在tableHeaderView中有了搜索栏和一个自定义工具栏(都是44px高度)。

我的工作方式是:

- (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);
}

所以我只是在输入搜索时将tableHeaderView设置为单个UISearchBar的大小,并在所有动画完成时将其设置回来。这解决了我的问题。甚至动画仍然有效(但它们不在childViewController中。不知道为什么)

相关问题