代码之家  ›  专栏  ›  技术社区  ›  Piepants

如何在添加为导航栏按钮项时更改搜索栏的高度

  •  0
  • Piepants  · 技术社区  · 7 年前

    我将在导航栏中添加一个搜索栏作为左按钮项。 我可以将搜索栏的宽度设置为任何我想要的,但是无论我尝试什么,高度总是一样的。 以下是一些代码:

    var searchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width * 0.7, height:28))
    
    let leftNavBarButton                    = UIBarButtonItem(customView:searchBar)  
    
    tabBarController?.navigationItem.leftBarButtonItem   = leftNavBarButton
    
    searchBar.showsCancelButton             = false
    searchBar.placeholder                   = NSLocalizedString("VC_SEARCH_PROMPT", comment: "")
    searchBar.delegate                      = self
    ...
    

    我可以通过UISearchBar构造函数中的width参数设置搜索栏的宽度,但更改height参数的值没有任何效果-搜索栏的高度始终相同。

    1 回复  |  直到 7 年前
        1
  •  1
  •   beyowulf    7 年前

    从iOS 11开始,您需要对尺寸栏按钮项目和标题视图使用约束。

    searchBar.heightAnchor.constraint(equalToConstant: 28).isActive = true
    searchBar.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width * 0.7).isActive = true // doubt this is the actual constraint you want though
    

    这是宣布的 here.