【问题标题】:Bring navbar in front of the UISearchController, Swift将导航栏放在 UISearchController 之前,Swift
【发布时间】:2016-08-24 03:55:05
【问题描述】:

我使用了一个通过 UISearchController 以编程方式创建的导航栏。
当我开始编辑 UISearchBar:

- 导航栏停留在昏暗的视图后面
- tableView 隐藏了一半的导航栏
- 当 tabeView 出现时,取消按钮不可选。



我的 UISearchController :

   let locationSearchTable = storyboard!.instantiateViewControllerWithIdentifier("SearchTableViewController") as!  SearchTableViewController
    resultSearchController = UISearchController(searchResultsController: locationSearchTable)
    resultSearchController.searchResultsUpdater = locationSearchTable
    resultSearchController.view.backgroundColor=UIColor.clearColor()
    locationSearchTable.delegate = self

    let searchBar = self.resultSearchController.searchBar
    searchBar.sizeToFit()
    searchBar.placeholder = "Search"
    searchBar.searchBarStyle = .Minimal
    searchBar.barStyle = .Default
    searchBar.translucent = true
    searchBar.barTintColor = UIColor.whiteColor()
    searchBar.setImage(UIImage(named:"search"), forSearchBarIcon: .Search, state : .Normal)
    searchBar.delegate=self
    resultSearchController.hidesNavigationBarDuringPresentation = false
    resultSearchController.dimsBackgroundDuringPresentation = true
    definesPresentationContext = true


我的导航栏:

let newNavBar : UINavigationBar = UINavigationBar.init(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 64.0))

func styleNavBar (){

    UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default
    self.navigationController?.setNavigationBarHidden(true, animated: false)


    let newItem : UINavigationItem = UINavigationItem.init()
    newItem.titleView = self.resultSearchController.searchBar

    newNavBar.barTintColor = UIColor(red: 243/255, green: 242/255, blue: 238/255, alpha: 1.0)
        newNavBar.translucent = false
        if let font = UIFont(name: "Avenir-Black", size: 16.0) {
            let navBarAttributesDictionary : [String : AnyObject]? = [
                NSForegroundColorAttributeName: UIColor(red: 74/255, green: 74/255, blue: 74/255, alpha: 0.51),
                NSFontAttributeName: font
            ]
            newNavBar.titleTextAttributes = navBarAttributesDictionary
        }

    newNavBar.setItems([newItem], animated: false)

    self.view.addSubview(newNavBar)
    self.view.bringSubviewToFront(newNavBar)
}

请问我该如何解决?
谢谢!

【问题讨论】:

  • 在你创建它们的地方显示更多代码会很好
  • @Tj3n 我更新了我的帖子! ;)

标签: ios swift uinavigationbar uisearchdisplaycontroller uisearchcontroller


【解决方案1】:

试试这个:

self.navigationController?.navigationBar.layer.zPosition = 1

【讨论】:

    【解决方案2】:

    你可以使用 stackview 。选择搜索和表格视图,然后从顶部/左侧/右侧/底部单击堆栈视图和约束堆栈视图

    【讨论】:

    • 好的。您可以创建 stackview 对象,而是将 newNavBar 添加到 stackview 和 locationSearchTable 中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-23
    • 2017-06-17
    • 2014-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    相关资源
    最近更新 更多