2017-04-12 26 views
0

在我的視圖控制器我編程方式添加一個搜索欄,它似乎正是我想要的方式,但是當我點擊它做什麼我不能寫/類型的東西搜索欄,並且日誌中沒有錯誤。 我添加的搜索欄在其他視圖控制器相同的方式,它的作品,但在那個特定的它不工作。我從前一個視圖控制器推動這個視圖控制器,因此它在左邊有一個導航返回按鈕(檢查圖像)它也可以工作。UISearchController搜索欄顯示,但不工作/觸發

在視圖控制器有一個TableView中和的CollectionView我想添加爲表視圖搜索。我想知道是否有這兩個在同一個視圖控制器中有什麼關係。

代碼中加入搜索欄

- (void)viewDidLoad { 
    //other stuff on view controller 
    [self setSearchView]; 
} 

-(void)setSearchView{ 
    //searchController is added as a property on .h file 
    //and all the delegate are also added 
    //@property (strong, nonatomic) UISearchController *searchController; 

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; 
    self.searchController.searchResultsUpdater= self; 
    self.searchController.dimsBackgroundDuringPresentation = NO; 
    self.searchController.searchBar.delegate = self; 
    self.navigationController.toolbarHidden=YES; 

    self.navigationItem.titleView=self.searchController.searchBar; 

    self.searchController.hidesNavigationBarDuringPresentation = NO; 
    self.definesPresentationContext = YES; 
} 

我來到這個視圖控制器從以前的視圖控制器,並單擊按鈕時按下此視圖控制器。而我使用榫文件
按鈕動作

- (void)ButtonClicked { 
    MYNextViewController *nextViewController = [[MYNextViewController alloc] initWithNibName:@"MYNibFileName" bundle:nil]; 

    // I also have a tab bar on the bottom which i am hiding for that view controller when pushing 
    nextViewController .hidesBottomBarWhenPushed=YES; 

    [self.navigationController pushViewController:nextViewController animated:YES]; 
} 

圖片:
enter image description here

任何幫助將不勝感激。希望我做我自己清楚......

+1

嘗試使用視圖herrachy調試並查看是否有阻止搜索欄或不 – Tj3n

+0

也許有在層次中的問題,我無法找到任何東西。我會看看..tnx –

+0

你有沒有試着去'[_ searchController becomeFirstResponder]''入法searchBarTextDidBeginEditing'呀 – Shahrukh

回答

1

最後我能解決這個問題,在視圖層次得益於一些變化@ Tj3n,但如果不知道是正確的解決方案,但現在它的工作.. 我也不得不刪除這條線,並添加一些更多的代碼爲其他目的

self.definesPresentationContext = YES; //removed... 

也回答的問題that是有幫助的

0

//請使用代碼

_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.navigationController.navigationBar.bounds.size.height)]; 
_searchBar.tintColor = [UIColor blueColor]; 

//[_searchBar setImage:[UIImage imageNamed:@"cross_icon"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal]; 
_searchBar.placeholder = @"Enter drug name"; 
//_searchBar.showsBookmarkButton = YES; 
_searchBar.barTintColor = [UIColor clearColor]; 

UITextField *txfSearchField = [_searchBar valueForKey:@"_searchField"]; 
txfSearchField.backgroundColor = [UIColor whiteColor]; 
txfSearchField.tintColor = [UIColor blackColor]; 
txfSearchField.textColor = [UIColor blackColor]; 
_searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth; 
_searchBar.delegate = self; 
_searchBar.showsCancelButton = NO; 
[_searchBar becomeFirstResponder]; 
[self.navigationController.navigationBar addSubview:_searchBar]; 
+0

不知道你的答案是如何與我的問題 –

相關問題