1

결과를 표시하기 위해 UISearchController를 사용하는 ViewController 하위 클래스를 작성하려고합니다. 뷰에는 사용자가 결과를 볼 수있는 최대 공간을 가질 수 있도록 화면 맨 위로 애니메이션을 적용하려는 입력 필드가 있습니다.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

UITearchController의 searchBar를 UITableView 헤더보기에 추가 할 때 비슷한 문제가 발생했습니다.

사용자가 searchBar를 선택한 후 UITableView를 보유하고있는 containerView의 제약 조건에 애니메이션을 적용하면 containerView를 따르지 않습니다.

내 경우에는 그 대리인 didPresentSearchController 후에 것으로 생각된다 : (UISearchController *) searchController를 호출, 검색 창은 UISearchBarContainerView의 수퍼 대신 jQuery과 헤더보기를했다.

애니메이션을 적용하기 전에 다시 원래의 헤더보기로 다시 추가하십시오.

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

    NSLog(@"did present search controller"); 

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

    ...animation... 

}