2013-10-25 57 views
0

我想知道有沒有什麼辦法來實現這樣的UI功能。 正如我們知道當我們在搜索欄中拖動和xcode 5中的searchDisplay控件時,我們會得到一組搜索控件,包括搜索欄和searchDisplayController。如何通過按鈕觸發searchDisplayController?

我想用一個按鈕來觸發搜索顯示控件,而不是使用本地搜索欄,而是像最新的Facebook應用程序那樣使用按鈕來觸發搜索顯示控件。

我試圖刪除搜索欄。但顯示控制直接失敗。 我試圖插入的tableView內的搜索欄(我不知道它的正確與否)

enter image description here

- (void)viewDidLoad 
{ 
    [self hideSearchBar]; 
} 

- (void)hideSearchBar 
{ 
    self.UISearchBar.hidden = YES; 
    self.UISearchBar.bounds = CGRectMake(0, 0, 0, 0); 
} 

- (IBAction)SearchClicked:(UIButton *)sender { 
    [self.searchDisplayController setActive:YES animated:NO]; 
    self.UISearchBar.hidden = NO; 
} 

- (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller 
{ 
    [self hideSearchBar]; 
} 

但它存在的問題是:

當我取消搜索滾動視圖不會返回到其以前的狀態。 (隱藏的搜索欄位於空白處)。

而不是看到這一點:

enter image description here

我想看看這個被點擊搜索視圖中的取消按鈕時。

enter image description here

我怎樣才能解決這個問題?

任何機構能否爲我提供更好的解決方案?

謝謝

回答

0

因此,最後,而不是拖動一個searchBar控件到故事板。 我正在創建並銷燬searchBarControl。

-(void)initialiseSearch 
{ 
    //Start 
    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 

    searchBar.delegate = self; 
    searchBar.showsCancelButton = YES; 
    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; 

    searchDisplayController.delegate = self; 
    searchDisplayController.searchResultsDataSource = self; 
    searchDisplayController.searchResultsDelegate = self; 

    [self.view addSubview:searchBar]; 

    searchBar.hidden = YES; 
    [searchDisplayController setActive:NO animated:NO]; 

    // [self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:@"CustomSearch" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"search"]; 
    //End 
} 

該方法在視圖加載中調用。

0

根據滾動視圖的原始中心創建一個CGPoint。 在viewDidLoad中():

self.originalCenter = scrollView.center; 

當用戶關閉搜索欄:

- (void)hideSearchBar 
    scrollView.center = self.originalCenter; 

滾動視圖哪裏是什麼你叫你的UIScrollView。

+0

它不按我想象的方式工作。 – TypingPanda

1

太簡單了!

-(void) searchBarCancelButtonClicked:(UISearchBar *)searchBar{ 
     [self.searchDisplayController setActive:NO animated:YES]; 
     self.navigationController popToViewController:UIViewControllerA animated:YES; 
    }