ios - UISearchDisplayController 搜索栏重叠导航栏并自行调整大小 IOS 7

标签 ios objective-c ios7 uitableview uisearchbar

我的应用程序有一个以表单形式呈现的表格 View Controller 。

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

旧版本的 IOS 一切正常,但在 IOS7 中,当用户点击搜索栏时,一切都搞砸了。

正常: enter image description here

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

搜索结束后: enter image description here

英寸.h

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

尝试了一些东西但是代码似乎没有改变任何东西,但是当放置断点时它进入委托(delegate)方法

英寸.米

//-(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

我也试过

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

我该如何解决这个问题?

最佳答案

尝试在您的 TableView Controller 中设置此值:

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

并更改您的 View 层次结构以拥有一个 View 、tableView、搜索栏和 nag 栏作为其 subview 。使您的 TableView Controller 成为 View Controller ,并将其作为数据源和委托(delegate)。

或者不使用 searchBarDisplayController 而只使用带有委托(delegate)方法的搜索栏:

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

关于ios - UISearchDisplayController 搜索栏重叠导航栏并自行调整大小 IOS 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19818998/

相关文章:

ios - Swift 中 TableViewCell 中的中心 iCarousel

ios - 使用标签在 TableView 中检索数据的问题

ios - NSString boundingRectWithSize 返回不必要的高度

打开新窗口时的 iOS UIWebView 事件

iphone - iOS 7 UINavigationBar 与 iOS 6 风格

ios7 - 拟合边界未按预期工作

objective-c - 如何判断 UILabel 何时将被截断和/或其换行位置将更改

ios - map View 不关注注释

iphone - 使用 setVolumeRampFromStartVolume 淡入淡出保存音频在 iOS 中不起作用

ios - 在 View 中绘制多个 UIBezierPath