ISearchController后的UIViewControl

ISearchController后的UIViewControl

本文介绍了UISearchController后的UIViewController大小错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIViewController,在此我在navigationbar中显示UISearchController.在ios11 . +中,这将增加navigationbar的高度.对我来说没关系.因此,用户单击我的按钮以显示searchbar,然后显示它.如果用户单击取消按钮,我将删除searchbar.当用户单击按钮以显示searchbar时,就会出现问题,此后,用户想要再走一个UIViewController.我加载了另一个UIViewController,并且在navigationbar和内容之间有一个黑条.我注销了视图的高度:

I have an UIViewController and in this I show UISearchController in the navigationbar. In ios11 . + this will increase the navigationbar height. That's okay for me. So the user click on my button to show the searchbar, and I show it. If the user click on the cancel button I remove the searchbar.The problem is occuring when the user click on the button to show the searchbar and after this, the user wants to go another UIViewController. I load the another UIViewController and I got a black bar between the navigationbar and the content.I logged out the height of the view:

这些是理想的尺寸:

viewDidLoad-视图大小:667.000000

viewDidLoad - view SIZE: 667.000000

viewWillAppear-导航栏大小:44.000000

viewWillAppear - NavBar SIZE: 44.000000

viewWillAppear-视图大小:603.000000

viewWillAppear - view SIZE: 603.000000

这些是错误的尺寸:

viewDidLoad-视图大小:667.000000

viewDidLoad - view SIZE: 667.000000

viewWillAppear-导航栏大小:44.000000

viewWillAppear - NavBar SIZE: 44.000000

viewWillAppear-查看大小:551.000000

viewWillAppear - view SIZE: 551.000000

您可以看到viewWillAppear方法中的视图高度小于预期的高度.为什么?我试图强制更新布局:

You can see that the height of the view in the viewWillAppear method is smaller than expected. Why?I tried to force update the layout:

[self.view layoutIfNeeded];
[self.view setNeedsLayout];

但是它不起作用.

推荐答案

ios11 +导航栏+搜索栏自启动以来始终会产生问题

ios11 + Navigation bar + Searchbar always creates problems since it launched

当我从具有搜索栏的View控制器中推View控制器时,当View Size不同时,我也遇到了类似的问题.并在导航栏

I have faced similar issue when View Size is different when I push View controller from View controller which has search bar. and shows BLACK BAR next to the Navigation bar

有多种解决方案可供选择,例如搜索栏"的固定高度,但其行为异常

There are number of solution available like fixed height of Search bar but it behaves unexpectedly

为此,我找到了创建UIView子类的解决方案

For that I found solution to create subclass of UIView

class SearchBarContainerView: UIView {

    let searchBar: UISearchBar

    init(customSearchBar: UISearchBar) {
        searchBar = customSearchBar
        super.init(frame: CGRect.zero)

        addSubview(searchBar)
    }

    override convenience init(frame: CGRect) {
        self.init(customSearchBar: UISearchBar())
        self.frame = frame
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        searchBar.frame = bounds
    }
}

然后将其用作标题视图

    // Search bar
    searchController = UISearchController(searchResultsController: nil)
    self.searchController.searchResultsUpdater = self
    self.searchController.delegate = self
    self.searchController.searchBar.delegate = self
    self.searchController.hidesNavigationBarDuringPresentation = false
    self.searchController.dimsBackgroundDuringPresentation = true
    self.searchController.searchBar.backgroundColor = .clear
    self.searchController.searchBar.placeholder = nil

    self.definesPresentationContext = true

    let searchBarContainer = SearchBarContainerView(customSearchBar: self.searchController.searchBar)

    searchBarContainer.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 44)
    navigationItem.titleView = searchBarContainer

希望这对您有帮助

这篇关于UISearchController后的UIViewController大小错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 06:39