以编程方式自动布局到 UISearchController 选择它时消失
Posted
技术标签:
【中文标题】以编程方式自动布局到 UISearchController 选择它时消失【英文标题】:Autolayout programmatically to UISearchController disappear when select it 【发布时间】:2016-05-03 17:34:16 【问题描述】:我有一个UIViewController
和一个UICollectionView
,并且我以编程方式添加了一个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
s 时它可以完美运行,但我真的需要 NSLayoutConstraint
s 因为我需要在两个方向上工作。
任何想法我做错了什么?
【问题讨论】:
交叉检查 UIView 约束并且你没有错过高度约束或者它会自动定义。 UIView 有高度限制 【参考方案1】:UISearchController
将从当前超级视图中删除 searchBar 并在其被激活时添加到_UISearchBarContainerView
。所以自动布局会失败。
下面的这个解决方法对我有用,使用 ReactiveCocoa(for hook) & Masonry(for 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
【讨论】:
【参考方案2】:在故事板中尝试以下步骤
-
删除搜索栏的所有约束,在视图控制器中也为框架或约束代码编写代码。
将搜索栏放入视图控制器,然后选择搜索栏并通过单击鼠标右键拖动左侧并选择视图的前导空间
通过单击鼠标右键并选择拖尾空格来拖动右侧
拖动顶部和底部并选择顶部和底部选项到您的视图。
//看到区别就可以轻松搞定了
【讨论】:
我知道如何在情节提要上添加带有自动布局的UISearchBar
。我需要使用 UISearchController
而不是 UISearchBar
这就是我以编程方式执行此操作的原因。
垃圾评论以上是关于以编程方式自动布局到 UISearchController 选择它时消失的主要内容,如果未能解决你的问题,请参考以下文章