简体   繁体   English

为什么我的 UISearchBar 在点击时会缩小?

[英]Why does my UISearchBar shrink when tapped?

When I load my code, the UISearchBar appears normally.当我加载我的代码时,UISearchBar 会正常显示。 However, after I tap the search field, the search bar shrinks and drops vertically lower.但是,在我点击搜索字段后,搜索栏会缩小并垂直下降。 Code:代码:

class MainViewController: UIViewController, UISearchBarDelegate {

    var searchBarController: UISearchController?
    var tableViewController: UITableViewController?
    var searchBar: UISearchBar?

    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = .blue

        let tableViewController = UITableViewController(style: .plain)
        let searchBarController = UISearchController(searchResultsController: tableViewController)

        self.searchBarController = searchBarController
        self.tableViewController = tableViewController

        self.searchBar = self.searchBarController?.searchBar
        self.searchBar?.frame = CGRect(x: 0, y: 50, width: self.view.frame.size.width, height: 75)
        self.searchBar?.searchBarStyle = .prominent
        self.view.addSubview(self.searchBar!)

    }    
}

Here is a visual before and after tapping:这是点击前后的视觉效果:

在此处输入图片说明

在此处输入图片说明

UIkit will resize the searchcontroller's view on layoutSubviews() which will reset your frame. UIkit 将在layoutSubviews()上调整搜索控制器视图的大小,这将重置您的框架。

If you want to control the size of the search bar independently, I'll suggest you make searchbar.translatesAutoresizingMaskIntoConstraints = false and/or in viewDidLayoutSubview() resize your searchbar with searchbar.frame = ...如果您想独立控制搜索栏的大小,我建议您使searchbar.translatesAutoresizingMaskIntoConstraints = false和/或在viewDidLayoutSubview()使用searchbar.frame = ...调整搜索栏的大小searchbar.frame = ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM