问题描述
我正在尝试在我的应用UI中放置一个UISearchController。布局是:
I'm trying to place a UISearchController within my apps UI. The layout is:
- 黄色:一个ViewController
- 红色:另一个ViewController
- 黑色:YellowViewController中的容器
我想将UISearchController的UISearchView放在黑色容器中。
I want to put the UISearchView of the UISearchController within the black container.
我的代码如下:
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsViewController];
UISearchBar* searchBar = self.searchController.searchBar;
searchBar.frame =_searchBarContainer.bounds;
[_searchBarContainer addSubview:searchBar];
[_searchBarContainer layoutIfNeeded];
它将UISearchBar放在正确的位置:
It places the UISearchBar in the correct place:
但是当我选择搜索字段时,它会扩展容器边界上的条形码:
But when I select the search field it expands the bar over the container's bounds:
如何解决这个问题并避免尺寸/外观变化何时被选中?
How can I solve that and avoid the size/appearance change when selected?
注意:尝试使用 translatesAutoresizingMaskIntoConstraints
和 clipToBounds 选项没有成功。我不是iOS UI的专家,所以我希望得到一个准确的答案。谢谢
Note: Tried some options playing with the translatesAutoresizingMaskIntoConstraints
and the clipToBounds
options with no success. I'm not an expert of iOS UI so I would appreciate an accurate answer. Thanks
推荐答案
根据我的研究,每次选择 SearchBar
,提供 UISearchController
。这个 UISearchController
总是试图让 searchBar
的宽度等于 UIViewController
呈现 UISearchController
。
According to my research, at each time you select SearchBar
, a UISearchController
is presented. This UISearchController
always try to make searchBar
's width equals to UIViewController
which is presenting UISearchController
.
我的解决方案是 UISearchController
使 SearchBar
框架错误,再次设置 SearchBar
'框架。您可以尝试以下代码。
My solution is when UISearchController
makes SearchBar
has wrong frame, set SearchBar
'frame again. You can try this code below.
@interface ViewController () <UISearchControllerDelegate>
@property (nonatomic, strong) UISearchController* searchController;
@property (weak, nonatomic) IBOutlet UIView *searchBarContainer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsViewController];
UISearchBar* searchBar = self.searchController.searchBar;
self.searchController.delegate = self;
searchBar.frame =_searchBarContainer.bounds;
[_searchBarContainer addSubview:searchBar];
[_searchBarContainer layoutIfNeeded];
}
- (void)willPresentSearchController:(UISearchController *)searchController {
[searchController.searchBar addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
- (void)willDismissSearchController:(UISearchController *)searchController{
[searchController.searchBar removeObserver:self forKeyPath:@"frame"];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if (object == self.searchController.searchBar) {
if (!CGSizeEqualToSize(self.searchController.searchBar.frame.size, _searchBarContainer.frame.size)) {
self.searchController.searchBar.superview.clipsToBounds = NO;
self.searchController.searchBar.frame = CGRectMake(0, 0, _searchBarContainer.frame.size.width, _searchBarContainer.frame.size.height);
}
}
}
@end
至少,它有效:)
或
- 你可以创建另一个
UIViewController
,其中包含SearchBar
,然后将其添加到_searchBarContainer,如果您的案例没有任何问题。 - 使用
UISearchBar
和UITableView
而不是UISearchController
。它更容易处理。
- You can create another
UIViewController
which containsSearchBar
after that add it to _searchBarContainer if your case don't have any problem with this. - Use
UISearchBar
andUITableView
instead ofUISearchController
. It's easier to handle.
这篇关于UISearchBar在单击时修改其框架/边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!