【问题标题】:How to Make autoLayout for UISearchController when rotate the device?旋转设备时如何为 UISearchController 制作自动布局?
【发布时间】:2015-05-08 19:56:35
【问题描述】:

我正在使用 UISearchController 与我的 tableview 集成。为了使我的 searchBar 不随表格滚动,我已经在导航栏下创建了一个视图(名称 SearchBarView),并且我的表格位于此视图下。我视图上的所有元素都使用自动布局进行设置:

在我的代码中。我制作了一个 UISearchController,就像 SearchBarView 的子视图,并使用 sideToFit 函数自动适应屏幕的宽度,如下所示:

      self.resultSeachController = ({
            let controller = UISearchController(searchResultsController: nil)
            controller.searchResultsUpdater = self
            controller.dimsBackgroundDuringPresentation = false
            controller.searchBar.sizeToFit()

//            add searchbar in the searchbarView
            self.searchBarView.addSubview(controller.searchBar)
            return controller
        })()

我的 SearchBar 现在工作完美,除了一个:当我旋转模拟器时,searchBar 不会自动适应屏幕的宽度。仅当我触摸 SearchBar 进行搜索时,它才适合屏幕。

我不知道如何解决。我试过这段代码:

    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {

        self.resultSeachController.searchBar.sizeToFit()

    }

但它仍然无法正常工作。我该如何解决?

【问题讨论】:

  • 你找到解决方法了吗?

标签: ios swift uisearchbar uisearchcontroller


【解决方案1】:

在制作动画的同时进行过渡:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
{
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        [_searchController.searchBar sizeToFit];
    } completion:nil];
}

【讨论】:

  • 就是这么简单!谢谢!
  • 另一种选择是:_searchController.searchBar.frame.size.width = size.width
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多