1

我想構建一個ViewController子類,它利用UISearchController來顯示結果。該視圖具有一個輸入字段,我想要在屏幕頂部進行動畫製作,以便用戶有足夠的空間查看結果。UISearchBar忽略編程約束

我用一個UIView構建了一個概念證明,它包裝了一個搜索欄插件組件,並在故事板中設置它們的頂部,前導,尾部和底部約束相等。以下代碼是負責動畫的不同的運動:

- (void)keyboardWillAppear:(NSNotification*)notification { 
    self.labelToSearchBarSpaceConstraint.active = NO; 
    self.searchBarAtTopConstraint.active = YES; 
    self.searchBarLeadingSpaceConstraint.constant = 0; 
    self.searchBarTrailingSpaceConstraint.constant = 0; 

    [UIView animateWithDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{ 
     [self.view setNeedsLayout]; 
     [self.view layoutIfNeeded]; 
    }]; 
} 

- (void)keyboardWillDisappear:(NSNotification*)notification { 
    self.searchBarAtTopConstraint.active = NO; 
    self.labelToSearchBarSpaceConstraint.active = YES; 
    self.searchBarLeadingSpaceConstraint.constant = 20; 
    self.searchBarTrailingSpaceConstraint.constant = 20; 

    [UIView animateWithDuration: [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{ 
     [self.view setNeedsLayout]; 
     [self.view layoutIfNeeded]; 
    }]; 
} 

我然後試圖切換到一個編程創建UISearchControllerUISearchBar。下面的代碼設置了什麼,我認爲是在代碼中,而不是在故事板的等量關係:

self.definesPresentationContext = YES; 

self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; 
UISearchBar* searchBar = self.searchController.searchBar; 
searchBar.translatesAutoresizingMaskIntoConstraints = NO; 
[self.searchBarView addSubview:searchBar]; 

[NSLayoutConstraint constraintWithItem:searchBar attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.searchBarView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0].active = YES; 
[NSLayoutConstraint constraintWithItem:searchBar attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.searchBarView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0.0].active = YES; 
[NSLayoutConstraint constraintWithItem:searchBar attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.searchBarView attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0.0].active = YES; 
[NSLayoutConstraint constraintWithItem:searchBar attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.searchBarView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0].active = YES; 

但是,做這樣當UISearchBar對象不服從的約束。當容器動畫時,搜索欄會飛離頁面。這是爲什麼發生?

微小更新:我注意到在NSLayoutConstraint的文檔中將其活動屬性設置爲YES而不是顯式添加它,但行爲完全相同。更新代碼以匹配。

回答

0

當將UISearchController的searchBar添加到UITableView標題視圖時,我遇到了類似的問題。

當用戶選擇searchBar後,在容器UITableView的容器視圖的動畫約束條件下,它不會跟隨容器視圖。

對我來說,它會出現委託didPresentSearchController後:(UISearchController *)searchController被調用時,搜索欄有UISearchBarContainerView的上海華代替的UITableView頭視圖。

在動畫幫助我之前,將它重新添加回原始標題視圖。

-(void)didPresentSearchController:(UISearchController *)searchController { 

    NSLog(@"did present search controller"); 

    self.tableView.tableHeaderView = self.searchController.searchBar; 

    ...animation... 

}