gpt4 book ai didi

ios - UISearchBar 忽略编程约束

转载 作者:搜寻专家 更新时间:2023-10-30 20:14:08 25 4
gpt4 key购买 nike

我正在尝试构建一个利用 UISearchController 来显示结果的 ViewController 子类。该 View 有一个输入字段,我想将其动画化到屏幕顶部,以便用户有最大的空间来查看结果。

我使用 UIView 构建了一个概念验证,该 UIView 包装了一个搜索栏下拉组件,并在 Storyboard 中将它们的顶部、前导、尾部和底部约束设置为相等。以下代码负责为不同的 Action 设置动画:

- (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。下面的代码建立了我认为是代码而不是 Storyboard 中的等效关系:

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 的文档中注意到将其 active 属性设置为 YES 而不是显式添加它,但行为完全相同。更新代码以匹配。

最佳答案

我在将 UISearchController 的搜索栏添加到 UITableView header View 时遇到了类似的问题。

当用户选择 searchBar 后持有 UITableView 的 containerView 的动画约束时,它不会跟随 containerView。

在我的例子中,在委托(delegate) didPresentSearchController:(UISearchController *)searchController 被调用之后,searchBar 有一个 UISearchBarContainerView 的 super View ,而不是 UITableView 标题 View 。

在动画帮助我之前将其重新添加回原始标题 View 。

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

NSLog(@"did present search controller");

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

...animation...

}

关于ios - UISearchBar 忽略编程约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39605138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com