【问题标题】:UISearchBar modifying its frame/bounds when clickedUISearchBar 在单击时修改其框架/边界
【发布时间】:2018-04-06 23:50:22
【问题描述】:

我正在尝试在我的应用程序 UI 中放置一个 UISearchController。布局是:

  • 黄色:一个 ViewController
  • 红色:另一个 ViewController
  • 黑色:YellowViewController 中的一个容器

我想将 UISearchController 的 UISearchView 放在黑色容器中。

我的代码如下:

self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsViewController];
UISearchBar* searchBar = self.searchController.searchBar;
searchBar.frame =_searchBarContainer.bounds;
[_searchBarContainer addSubview:searchBar];
[_searchBarContainer layoutIfNeeded];

它将 UISearchBar 放置在正确的位置:

但是当我选择搜索字段时,它会在容器的边界上展开栏:

我该如何解决这个问题并避免在选择时改变尺寸/外观?

注意:尝试了一些使用 translatesAutoresizingMaskIntoConstraintsclipToBounds 选项的选项,但没有成功。我不是 iOS UI 的专家,所以我会很感激一个准确的答案。谢谢

【问题讨论】:

  • 你试过设置固定宽度还是固定最大宽度?
  • 在将 searchBar 添加到子视图之前尝试设置 searchBar.frame。
  • @SteeBono 在示例代码中我设置了 searchBar 框架之前:(

标签: ios objective-c xcode interface-builder


【解决方案1】:

根据我的研究,每次选择SearchBar,都会出现一个UISearchController。这个UISearchController 总是试图使searchBar 的宽度等于UIViewController 呈现UISearchController

我的解决方案是当UISearchController 使SearchBar 有错误的框架时,再次设置SearchBar'frame。你可以试试下面这段代码。

@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

至少,它有效:)

或者

  • 如果您的情况对此没有任何问题,您可以创建另一个包含SearchBarUIViewController,然后将其添加到_searchBarContainer。
  • 使用UISearchBarUITableView 而不是UISearchController。它更容易处理。

【讨论】:

  • 为我工作。谢谢!
【解决方案2】:

我找到了有用的信息。

点击 UISearchBar 时会调用多种方法。 当调用此方法的某些部分时,UISearchBar 的框架会更改其值。

其中一种方法尝试填充等于UIViewController 的宽度。

尝试在以下方法之一中设置帧值:

searchBarTextDidBeginEditing
searchBarShouldBeginEditing

这样你就覆盖了默认值。

再见

【讨论】:

    猜你喜欢
    • 2011-02-22
    • 1970-01-01
    • 2014-07-05
    • 2014-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    相关资源
    最近更新 更多