2013-10-19 49 views
19

我有一個UIViewController,我想用serchBar顯示一個tableview。就這麼簡單:UISearchBar動畫問題

//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欄裏面點擊使動畫的開始和它看起來像它有一個20像素的不必要偏移

+0

你可以加我到NSChat組嗎?還要在IB中試驗這些維度,看看問題是否仍然存在。什麼尺寸可以消除這個問題也可以從IB – aMother

+0

推斷Im not use IB – luca

+0

您是否在使用任何種類的動畫? @luca –

回答

1

提醒任何有類似問題的人。我需要添加這條固定東西的線:

self.edgesForExtendedLayout = UIRectEdgeNone; 
-2

我已申請代碼問題發生,,它的工作原理對我而言,只要隱藏你的導航欄,並從y = 20開始搜索欄,而不是y = 0;

+0

您可以發送一個鏈接到您的測試,以便我可以比較請 – luca

+0

https://dl.dropboxusercontent.com/u/218312890/SandySearchBarTest.zip –

+0

您的測試中沒有導航欄 – luca

15

在你故事板,選擇有問題的控制器,看看屬性選項卡,並試圖修改此設置:

  • 在最熱門酒吧
  • 在不透明酒吧

我已經解決與這種設置不相關的問題類似。

+0

我添加了一個鏈接到我的項目..檢查出來 – luca

+0

偉大的一個!那就是訣竅! –

+1

很好的解決方案。我發現唯一的問題是,視圖控制器的導航欄顏色會有所不同(因爲沒有更透明的疊加層),所以如果您有多個視圖控制器,您可能想要將它們全部更改爲保持一致性 –

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

,我只是embade我用的UINavigationController及其合作得非常好控制器.. enter image description here

0

你爲什麼要在故事板創建搜索欄編程方式而不是嗎? 我目前使用的是搜索欄,添加了故事板,它的工作正常(我必須更改contentOffset)

+0

你是什麼意思改變內容偏移量?你能發佈你的代碼片段嗎? – luca

+0

是的,就是這樣: ' - (void)viewDidLayoutSubviews {self.tableView setContentOffset:CGPointMake(0,-60)]; }' – CZ54

8

我發現是什麼原因導致此問題。當您將navigationBar.translucent設置爲NO時,似乎動畫會混亂。如果你使導航條半透明,一切都應該正常工作,但這絕對不是一個理想的解決方案。我將嘗試尋找解決方法。

1

您可以通過繼承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]; 
    } 
} 
1

codyko給了我一個想法。這是因爲導航欄不是半透明的。所以我將它設置爲半透明這個視圖控制器上休息,我離開的時候用下面的代碼:

- (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相同的顏色作爲我的導航欄就在它後面讓它看起來不透明。我知道這不是完美的,但它很好地工作。