【问题标题】:Make SearchBar higher in navigation bar在导航栏中使 SearchBar 更高
【发布时间】:2018-02-06 21:48:07
【问题描述】:

我尝试创建一个搜索栏,现在它工作正常,但我不知道如何让它更高一点,就在状态栏下方。 搜索栏是通过编程方式创建的,我使用了这段代码:

let searchController = UISearchController(searchResultsController: nil)




override func viewDidLoad() {
    super.viewDidLoad()

    searchController.searchResultsUpdater = self
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.hidesNavigationBarDuringPresentation = false
    definesPresentationContext = true
    navigationItem.searchController = searchController
    navigationItem.hidesSearchBarWhenScrolling = false
}

【问题讨论】:

    标签: cocoa-touch uinavigationbar uisearchbar uinavigationitem uisearchcontroller


    【解决方案1】:

    您的意思是,您希望在导航栏的位置有一个搜索栏。如果是这种情况,请尝试在导航栏中添加搜索栏。

    试试这个

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

    你保留了一个懒惰的 UISearchBar 属性

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

    在 viewDidLoad 中

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

    如果您想使用故事板,只需将您的搜索栏拖动为插座,然后将惰性属性替换为您的插座搜索栏

    或者简单

    // create the search bar programatically since you won't be
    // able to drag one onto the navigation bar
    
    
    searchBar = UISearchBar()  
    searchBar.sizeToFit()
    
    
    // the UIViewController comes with a navigationItem property
    // this will automatically be initialized for you if when the
    // view controller is added to a navigation controller's stack
    // you just need to set the titleView to be the search bar
    
    navigationItem.titleView = searchBar
    

    【讨论】:

    • 是的,代码 navigationItem.titleView = searchBar 工作正常,搜索栏现在就在状态栏下方,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多