限制iOS 11中UISearchBar的高度 [英] Limit the height of UISearchBar in iOS 11

查看:67
本文介绍了限制iOS 11中UISearchBar的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS 11中,UISearch栏的高度增加了,但是我希望与iOS 10中的高度相同.该怎么做?我正在使用以下代码创建searchController.

In iOS 11 height of UISearch bar has increased , but I want the same height as in iOS 10. How to do that ? I am using below code to create the searchController.

    searchController = UIUtils.searchControllerInitialize(self)
    searchController.searchResultsUpdater = self
    searchController.delegate = self
    searchController.searchBar.delegate = self

    viewTemp = UIView(frame: CGRect(x: 0.0, y: 64.0,width: UIScreen.main.bounds.size.width , height: 44))
    viewTemp.addSubview(self.searchController.searchBar)
    self.view.addSubview(viewTemp);


    class func searchControllerInitialize(_ forViewController: UIViewController) -> UISearchController  {
    let controller = UISearchController(searchResultsController: nil)
    controller.hidesNavigationBarDuringPresentation = true
    // This property dismiss the background the navigation bar
    controller.dimsBackgroundDuringPresentation = false
    controller.definesPresentationContext = true
    forViewController.definesPresentationContext = true
    controller.searchBar.sizeToFit()

    let topView: UIView = controller.searchBar.subviews[0] as UIView
    for subView in topView.subviews {
        if subView.isKind(of: NSClassFromString("UITextField")!) {
            (subView as! UITextField).returnKeyType = UIReturnKeyType.search
            (subView as! UITextField).enablesReturnKeyAutomatically = true
        }
    }

    let viewS = UIView(frame: CGRect(x: 0.0, y: 0.0,width: UIScreen.main.bounds.size.width , height: 64))
    viewS.backgroundColor = UIColor.DTColor()
    controller.view.addSubview(viewS)
    controller.hidesNavigationBarDuringPresentation = false

    if #available(iOS 11.0, *) {
        controller.searchBar.translatesAutoresizingMaskIntoConstraints = false
        controller.searchBar.heightAnchor.constraint(equalToConstant: 44).isActive = true
    }

    return controller
}

推荐答案

我在我的应用程序上也遇到了同样的问题.它在iOS 10中完美运行.

I met the same issue on my app, too. It works perfectly in iOS 10.

但是,它会像这样在iOS11中跳出我的视野.

However, it will jump out of my view in iOS11 as like this.

我也不想将搜索栏放到navigationItem中.

Also I don't want to put my search bar into navigationItem.

所以也许您可以考虑我的答案.就是这个.

So maybe you can consider my answer. Here is it.

在故事板中:

代码:

override func viewDidLoad() {

    super.viewDidLoad()

    self.setupSearchController()

}

func setupSearchController() {

    self.searchResultController = UISearchController(searchResultsController: nil)

    self.searchResultController.searchResultsUpdater = self

    self.searchResultController.delegate = self

    self.searchResultController.hidesNavigationBarDuringPresentation = false

    self.searchResultController.dimsBackgroundDuringPresentation = false

    self.searchResultController.searchBar.delegate = self

    self.searchResultController.searchBar.searchBarStyle = .minimal

    self.searchResultController.searchBar.tintColor = UIColor.white

    self.phoneSearchView.searchBarContainer.addSubview(self.searchResultController.searchBar)

    self.memberListTableView.tableHeaderView = self.phoneSearchView

}

override func viewWillLayoutSubviews() {

    super.viewWillLayoutSubviews()

    self.searchResultController.searchBar.sizeToFit()

    self.searchResultController.searchBar.frame.size.width = self.phoneSearchView.searchBarContainer.frame.size.width

    self.searchResultController.searchBar.frame.size.height = self.phoneSearchView.searchBarContainer.frame.size.height

}

现在,它就像一种魅力.

Now it works like a charm.

这篇关于限制iOS 11中UISearchBar的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆