以编程方式对UISearchController的Autolayout在选择时消失

时间:2016-05-03 17:34:16

标签: ios swift autolayout uicollectionview uisearchcontroller

我有一个带有`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])

当我点击在搜索栏上搜索时,它完全消失了:

SearchBar disappear

当我旋转设备时,我收到此错误:

"<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,因为我需要在两个方向上工作。

任何想法我做错了什么?

2 个答案:

答案 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)

在故事板中尝试以下步骤

  1. 删除搜索栏中的所有约束,并在视图控制器中也为帧或约束代码编写代码。
  2. 将搜索栏放入视图控制器,然后选择搜索栏并左键单击鼠标右键并选择前导空格到视图中
  3. 通过单击鼠标右键并选择尾随空格到视图来拖动右侧
  4. 拖动顶部和底部,然后为视图选择顶部和底部选项。
  5. //看到差异,然后就可以轻松完成了