【问题标题】:swift iOS13 searchBar textfield height changeswift iOS13 searchBar 文本字段高度变化
【发布时间】:2019-10-16 09:21:18
【问题描述】:

有机会改变UISearchBarUITextField 高度吗? 我尝试了几乎所有我能找到的解决方案,但都没有成功。

更新: 我意识到ios13 searchBar不包含UITextField,但包含UISearchBarTextField

这里是层次结构

据我了解,我们必须寻找 iOS13 的新类而不是 UITextField

我试过循环和搜索 TextField

    override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    for subView in searchBar.subviews {
        for subsubView in subView.subviews{
            for subsubsubView in subsubView.subviews{
                if #available(iOS 13.0, *) {
                    if let textField = subsubsubView as? UISearchTextField{
                        var bounds: CGRect
                        bounds = textField.frame
                        bounds.size.height = 40 //(set height whatever you want)
                        textField.bounds = bounds
                        textField.layoutIfNeeded()
                    }
                } else {
                    if let textField = subsubView as? UITextField {
                        var bounds: CGRect
                        bounds = textField.frame
                        bounds.size.height = 40 //(set height whatever you want)
                        textField.bounds = bounds

                    }
                }
            }
        }
    }
}

调试器进入状态,发现 UITextField 并改变了高度,但在设备上 - 没有高度变化

【问题讨论】:

  • 您是否尝试过循环 UISearchBar 的子视图并更改 UITextField(这是子视图之一)的高度?
  • 你用什么方法做的改变?您是否尝试过触发 layoutIfNeeded..?
  • 我们可以对文本字段应用比例变换吗?
  • @denis_lor 是的。我已经尝试循环和 layoutIfNeed 调用并尝试在 willViewAppear 中更改高度。没有任何帮助

标签: ios swift


【解决方案1】:

我最终所做的是将 UIImageView 设置为 UISearchBar 的容器。

不适用于简单的 UIView,我猜它与内在内容大小有关?

    func showSearchBar() {

        self.searchNavigationItem?.setRightBarButton(nil, animated: false)


        guard let searchBar = self.searchBar else {

            assert(false, "Need a search bar to display")
            return
        }


        self.searchContainer = UIImageView()
        self.searchContainer?.isUserInteractionEnabled = true

        let image = // Some image with the corresponding size.


        self.searchContainer?.image = image
        self.searchContainer?.alpha = 0

        self.searchContainer?.frame = // Same size as the image

        // Here you add the searchContainer into the titleView.
        self.searchNavigationItem?.titleView = self.searchContainer

        searchBar.translatesAutoresizingMaskIntoConstraints = false

        // Create top/bottom/leading/trailing constraints

        self.searchContainer?.addSubview(searchBar)
        searchBar.sizeToFit()

        self.searchContainer?.addConstraints([topConstraint, bottomConstraint, leadingConstraint, trailingConstraint])

         self?.searchContainer?.alpha = 1

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-03
    • 2020-01-23
    • 2018-06-06
    相关资源
    最近更新 更多