Uitableview UISearchBar动画问题

Uitableview UISearchBar动画问题,uitableview,ios7,uisearchbar,uisearchdisplaycontroller,Uitableview,Ios7,Uisearchbar,Uisearchdisplaycontroller,我有一个UIViewController,我想在其中用serchBar显示一个tableview //viewDidLoad _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCR

我有一个UIViewController,我想在其中用serchBar显示一个tableview

//viewDidLoad
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,
                                                           0, 
                                                           SCREEN_WIDTH(),
                                                           SCREEN_HEIGHT())
                                                    style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;

[self.view addSubview:_tableView];

// adding uisearch bar
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

_tableView.tableHeaderView = searchBar;

//
 searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;

当我在uisearch栏中单击时,会出现此问题,这样动画就会启动,看起来它有一个20px的不需要的偏移量。

我已经应用了您的代码,它对我很好,只需隐藏导航栏,然后从y=20开始搜索栏,而不是y=0

在情节提要中,选择有问题的控制器,查看属性选项卡并尝试编辑以下设置:

  • 顶杆下
  • 不透明条下
我通过坚持不懈的这些设置解决了类似的问题

UITableView *_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,
                                                           64,
                                                           self.view.frame.size.width,
                                                           self.view.frame.size.height)
                                          style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;

[self.view addSubview:_tableView];

// adding uisearch bar
UISearchBar* searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

_tableView.tableHeaderView = searchBar;



//
UISearchDisplayController* searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;

我刚刚用UINavigationcontroller制作了我的控制器,它工作得很好。

为什么要用编程方式而不是在故事板中创建搜索栏?
我目前正在使用添加在情节提要中的搜索栏,它工作正常(我必须更改contentOffset)

我找到了导致此问题的原因。当您将navigationBar.translucent设置为否时,动画似乎会变得一团糟。如果将navigationBar设置为半透明,则一切都会正常工作,但这绝对不是理想的解决方案。我将尝试找到解决方法。

您可以通过子类化UISearchDisplayController并添加以下内容来取消动画:

- (void)setActive:(BOOL)visible animated:(BOOL)animated
{
     if(self.active == visible) return;
     [self.searchContentsController.navigationController setNavigationBarHidden:YES animated:NO];
     [super setActive:visible animated:animated];
     [self.searchContentsController.navigationController setNavigationBarHidden:NO animated:NO];
     if (visible) {
          [self.searchBar becomeFirstResponder];
     } else {
          [self.searchBar resignFirstResponder];
     }
}
给了我一个主意。这是因为导航栏不是半透明的。因此,在这个视图控制器上,我将其设置为半透明,并在我离开时使用以下代码进行休息:

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.translucent = NO;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = YES;
}

现在,该控制器上的导航栏略微褪色,因此我添加了一个与导航栏颜色相同的
UIView
,使其看起来不透明。我知道它并不完美,但效果很好。

提醒任何有类似问题的人。我需要添加这一行来修复问题:

self.edgesForExtendedLayout = UIRectEdgeNone;

你能把我加入NSChat小组吗。同时在IB中测试这些维度,看看问题是否仍然存在。从你是否使用任何类型的动画可以推断出什么维度可以消除这个问题@卢卡诺。。刚刚在tableview标题中添加了UIsearchbar。请发送一个指向测试的链接,以便我可以进行比较。你的测试中没有导航栏。我正在应用程序中使用导航栏。我认为这不是我添加项目链接的方式。。检查一下这个很好的解决方案。我发现的唯一问题是视图控制器的导航栏颜色会有所不同(因为没有更透明的覆盖),因此如果您有多个视图控制器,您可能希望全部更改以保持一致性,并且故事板会再次罢工!更改内容偏移量是什么意思?你能发布你的代码片段吗?是的,就是这样:
-(void)viewdilayoutsubviews{[self.tableView setContentOffset:CGPointMake(0,-60)];}