我有一个带有`UICollectionView的UIViewController
,我已经以编程方式添加了一个UISearchController:
@IBOutlet weak var searchBarPlaceholder: UIView!
-------------------------------------------------
...
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.sizeToFit()
searchBarPlaceholder.addSubview(searchController.searchBar)
automaticallyAdjustsScrollViewInsets = false
definesPresentationContext = true
searchController.delegate = self
searchController.searchBar.translatesAutoresizingMaskIntoConstraints = false
let leadingConstraint = NSLayoutConstraint(item: searchController.searchBar, attribute: .Leading, relatedBy: .Equal, toItem: searchBarPlaceholder, attribute: .Leading, multiplier: 1, constant: 0) // add margin
let trailingConstraint = NSLayoutConstraint(item: searchController.searchBar, attribute: .Trailing, relatedBy: .Equal, toItem: searchBarPlaceholder, attribute: .Trailing, multiplier: 1, constant: 0)
let topConstraint = NSLayoutConstraint(item: searchController.searchBar, attribute: .Top, relatedBy: .Equal, toItem: searchBarPlaceholder, attribute: .Top, multiplier: 1, constant: 0)
self.searchBarPlaceholder.addConstraints([leadingConstraint, trailingConstraint, topConstraint])
当我点击在搜索栏上搜索时,它完全消失了:
当我旋转设备时,我收到此错误:
"<NSAutoresizingMaskLayoutConstraint:0x7fc499782ee0 h=-&- v=--& _UISearchBarContainerView:0x7fc49b9cb3f0.width == _UISearchControllerView:0x7fc4997603b0.width - 667>",
"<NSLayoutConstraint:0x7fc4997655c0 'UIView-Encapsulated-Layout-Width' H:[_UISearchControllerView:0x7fc4997603b0(375)]>"
我还添加了UISearchControllerDelegate
方法,但它不起作用:
func didPresentSearchController(searchController: UISearchController) {
if searchController.searchBar.superview == nil {
for searchCtrlChildView in searchController.view.subviews {
if searchCtrlChildView.frame.origin == CGPoint(x: 0, y: 0) {
searchCtrlChildView.addSubview(searchController.searchBar)
break
}
}
}
}
但是,当我移除NSLayoutConstraint
时,它完美无缺,但我确实需要NSLayoutConstraint
,因为我需要在两个方向上工作。
任何想法我做错了什么?
答案 0 :(得分:0)
UISearchController
会从当前超级视图中删除searchBar,并在激活后添加到_UISearchBarContainerView
。因此autolayout将失败。
下面的解决方法适用于我,使用ReactiveCocoa(for hook)&amp;砌体(用于setConstraint)
@weakify(self);
[[[self.searchController.searchBar
rac_signalForSelector:@selector(didMoveToSuperview)]
filter:^BOOL(id value) {
@strongify(self);
return self.searchController.searchBar.superview != nil;
}]
subscribeNext:^(id x) {
@strongify(self);
//fix wrong layout when controller is actived
UISearchBar *searchBar = self.searchController.searchBar;
[searchBar mas_remakeConstraints:^(MASConstraintMaker *make) {
make.leading.and.trailing.and.top.equalTo(searchBar.superview);
if (searchBar.superview != self.view) {
//use low level priority to ensure works on future
make.top.and.bottom.equalTo(searchBar.superview).priority(UILayoutPriorityFittingSizeLevel-1);
}
}];
//fix wrong layout when navigationBar.translucent=NO
if (searchBar.superview != self.view) {
UIView *view = searchBar.superview;
[view mas_remakeConstraints:^(MASConstraintMaker *make) {
make.leading.and.trailing.and.top.equalTo(view.superview).priority(UILayoutPriorityFittingSizeLevel-1);
}];
}
}];
并确保self.definesPresentationContext
设置为YES
答案 1 :(得分:-1)
在故事板中尝试以下步骤
//看到差异,然后就可以轻松完成了