【问题标题】:SearchDisplayController SearchBar overlapping navigation bar and resizing itself IOS7UISearchDisplayController 搜索栏重叠导航栏并调整自身大小 IOS 7
【发布时间】:2013-11-06 17:42:49
【问题描述】:

我的应用程序有一个表格视图控制器,它显示为表单。

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

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

正常:

当用户开始输入时:

搜索结束后:

在.h

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

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

英寸米

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

根据断点,框架位置和大小是正确的,但它们不会改变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];
    }
}

我该如何解决这个问题?

【问题讨论】:

  • 你添加了搜索栏或搜索显示控制器吗?
  • 它是一个搜索栏显示控制器
  • 你试过我的解决方案了吗?
  • 导航栏在表格视图中的任何原因?

标签: ios objective-c ios7 uitableview uisearchbar


【解决方案1】:

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

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

然后将视图层次结构更改为具有视图、tableView、搜索栏和导航栏,因为它是子视图。让你的表视图控制器成为视图控制器,并使其成为数据源和委托。

或者不要使用 searchBarDisplayController,而只使用带有代理方法的搜索栏:

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

【讨论】:

  • 我在发布我的问题之前已经尝试过这个,它不起作用,代码添加到问题
  • 非常感谢! self.edgesForExtendedLayout = .None 就像一个魅力!
【解决方案2】:

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

我已经解决了这个问题:

- (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 中没有。不知道为什么)

【讨论】:

    猜你喜欢
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-30
    • 2012-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多