How to change height of search bar? Swift

谁都会走 提交于 2019-12-22 08:25:40

问题


I have this:

http://postimg.cc/image/oepn8hhzd/

and I need this one:

http://postimg.cc/image/4bacislw5/

My code of search bar

var searchBar:UISearchBar = UISearchBar(frame: CGRectMake(0, 0, 200, 0))


    searchBar.tintColor = UIColor.blueColor()
    self.searchBar.layer.borderColor = UIColor.blueColor().CGColor
    self.searchBar.layer.borderWidth = 0.5
    self.searchBar.layer.cornerRadius = 5.0
    self.searchBar.clipsToBounds = true
    searchBar.placeholder = "Search for cities"
    searchBar.sizeToFit()


    self.searchBar.setImage(UIImage(named: "Search@3x"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal)
    self.searchBar.setImage(UIImage(named: "Close@3x"), forSearchBarIcon: UISearchBarIcon.Clear, state: UIControlState.Normal)

    searchBar.delegate = self


    var leftNavBarButton = UIBarButtonItem(customView: searchBar)
    self.navigationItem.leftBarButtonItem = leftNavBarButton

Any ideas? (Sorry for links, I cannot insert images)


回答1:


You can use another view as navigation bar title and place search bar inside it.

let searchController = UISearchController(searchResultsController: nil)
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.delegate = self

let frame = CGRect(x: 0, y: 0, width: 100, height: 44)
let titleView = UIView(frame: frame)
searchController.searchBar.backgroundImage = UIImage()
searchController.searchBar.frame = frame
titleView.addSubview(searchController.searchBar)
navigationItem.titleView = titleView

Hope this will help.



来源:https://stackoverflow.com/questions/31446458/how-to-change-height-of-search-bar-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!