2014-02-19 55 views
13

我有一個UITableViewController T,其中包含標準方式的UISearchDisplayControllerUISearchBar(在nib中定義的所有內容)。然後T被其他視圖控制器以標準方式包含在子視圖控制器中。UISearchBar向下移動20個像素

搜索欄顯示正確:

normal search bar

但是,當我點擊搜索欄中的確需要搜索,搜索欄變得更高,和搜索欄裏面的內容由什麼樣子滴下來約20像素(呃,狀態欄巧合的高度?)像這樣:

weird search bar is too tall

什麼與怎麼了?它太高了。此外,使它看起來像這樣的動畫是不明智的。任何方法來防止這種難看的增長?

+1

我在這個問題'UISearchController'你還記得,如果/您是如何解決的呢? – AnthonyM

回答

7

我設法解決一個非常類似的問題給你通過編程設置UISearchController像這樣經歷了一個:

注〜如果hidesNavigationBarDuringPresentation沒有被設置爲NO的錯誤仍然存​​在。

#pragma mark - View Life Cycle 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.results = [[NSArray alloc] init]; 

// 1 \\ Results Table View 
    UITableView *searchResultsTableView = [[UITableView alloc] initWithFrame:self.tableView.frame]; 
    [searchResultsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:Identifier]; 
    searchResultsTableView.delegate = self; 
    searchResultsTableView.dataSource = self; 

// 2 \\ init search results table view & setting its table view 
    self.searchResultsTableViewController = [[UITableViewController alloc] init]; 
    self.searchResultsTableViewController.tableView = searchResultsTableView; 
    self.searchResultsTableViewController.view.backgroundColor = [UIColor blackColor]; 

// 3 \\ init a search controller with it's tableview controller for results 
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchResultsTableViewController]; 
    self.searchController.hidesNavigationBarDuringPresentation = NO; // √ 
    self.searchController.searchResultsUpdater = self; 
    self.searchController.delegate = self; 
    self.searchController.searchBar.barTintColor = [UIColor blackColor]; 

// 4 \\ Make an appropriate search bar (size, color, attributes) and add it as the header 
    [self.searchController.searchBar sizeToFit]; 
    self.tableView.tableHeaderView = self.searchController.searchBar; 
    self.tableView.tableHeaderView.backgroundColor = [UIColor blackColor]; 

// 5 \\ Enable presentation context 
    self.definesPresentationContext = YES; 
    self.tableView.backgroundColor = [UIColor clearColor]; 
    self.currentUser = [PFUser currentUser]; 
} 

增加更多詳情:

  1. Interface Builder中:的UIViewController(不UITableViewController中)
  2. 添加UITableView與約束:

    一個。 enter image description here

    b。 *我的導航欄是定製的高度 - 你的「頂層空間到:頂面佈置指南」可以改變

  3. 從添加表視圖中添加代表&數據源網點到的UIViewController

    一。不要忘了這樣做編程:

    @interface AddUsersViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 
    
    @property (weak, nonatomic) IBOutlet UITableView *tableView; 
    
  4. 在你接口擴展

    一個。設置你的UISearchConroller的委託和數據源:

    #import "AddUsersViewController.h" 
    
    #define ResultsTableView self.searchResultsTableViewController.tableView 
    #define Identifier @"Cell" 
    
    @interface AddUsersViewController() <UISearchControllerDelegate, UISearchResultsUpdating> 
    
    @property (nonatomic) UISearchController *searchController; 
    @property (nonatomic) UITableViewController *searchResultsTableViewController; 
    @property (nonatomic, weak) UIBarButtonItem *leftBarButton; 
    @property (nonatomic) NSArray *results; 
    
    @end 
    

雖然這個工作對我來說,我已經發現,有可能會造成這個其他可能出現的情況。我的「問題」並不是搜索欄向下移動了20px(或左右),而是移動了。我將其歸因於包含圖像標題和自定義NavBarButtons的自定義UINavigationBar標題。考慮到這一點,這裏有兩種可能的解決方案:

原方案:

  1. 你的容器視圖的extendedLayoutIncludesOpaqueBars物業

    一個。 故事板檢查√在故事板中的表視圖「不透明酒吧」下。

    b。 以編程方式yourTableView(s).extendedLayoutIncludesOpaqueBars = YES;

  • 設置scopeButtonTitles空數組。 (如下圖)

    self.searchController = UISearchController(searchResultsController: nil); 
    self.searchController.searchResultsUpdater = self; 
    self.searchController.searchBar.scopeButtonTitles = []; 
    self.tableView.tableHeaderView = self.searchController.searchBar; 
    
  • 後兩種答案來自THIS問題,可能被證明是太檢查出有益的。

    感謝BananaNeil的研究和測試,他發現如果您按照我的建議設置searchController.searchBar.y = 0,那麼您的搜索欄應該完全位於您期望的位置。 √

    +0

    感謝您爲此付出的所有努力。我正在解決您的解決方案,並且一定會越來越近,但仍然存在問題。如果我能爲它工作,我會將其標記爲已接受。 – BananaNeil

    +0

    你在使用'UISearchDisplayController'還是'UISearchController'?前者在iOS 8中被棄用,可能是問題的根源。 – ChrisHaze

    +0

    @BananaNeil我已經添加了我剛剛提供的'viewDidLoad:'方法之外實現的代碼/工作。 – ChrisHaze

    0

    而不是使用UITableViewController,選擇一個正常的UIViewController,並添加一個UITableView到它的視圖。添加約束將其固定到Top Layout Guide.BottomSuperview.LeadingSuperview.Trailing,Bottom Layout Guide.Top。然後將Search Bar添加到UITableView。

    由於某些原因,搜索欄在執行時不會跳轉。

    一個可能的原因可能是編輯搜索控制器時,通過偏移頂部佈局指南的高度來調整搜索欄的位置。明確固定桌面視圖頂部到Top Layout Guide.Bottom有助於某種方式。

    希望這會有所幫助!

    +0

    我很確定搜索顯示控制器已被棄用。 – BananaNeil

    +0

    已棄用。但是由於原來的問題提到它,我繼續採用這種方法。 –

    +0

    但是,即使使用UISearchController,此解決方案也適用於您。 –

    1

    其實我已經想通了,我的問題:

    我有我的UISearchBar內部的包裝來看,這點我是重新定位爲滾動的UITableView

    包裝是20px高於搜索欄,我設置searchbar.y = 20(出於文體的原因)。每次我點擊搜索欄時,它莫名其妙地增長了20px。

    在某一點上,我設置了searchBar.y = 10,並注意到它僅增長了10px

    果然,事實證明searchBar的增長高度與其立即超級檢視中的y位置完全相同。所以,當我設置searchbar.y = 0時,搜索欄停止增長。

    6

    不知道這是iOS的9個特定,但設置

    searchController.hidesNavigationBarDuringPresentation = NO;

    解決所有在包含UITableViewUISearchBar的怪異向下運動的問題。當然,這確實禁用導航欄 - 所以這可能不是你想要的。