如何摆脱UINavigationController和UISearchBar之间的小差距?

时间:2015-08-15 01:34:54

标签: ios swift uinavigationbar uisearchbar uisearchcontroller

enter image description here

如何消除导航栏和搜索栏之间的这个小差距?

以下是自定义导航栏和搜索栏外观的代码:

 self.navigationController?.navigationBar.barStyle = .Black
    self.navigationController?.navigationBar.barTintColor = UIColor(red: 41/255, green: 128/255, blue: 185/255, alpha: 1.0)
    self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
    let navigationTitleFont = UIFont(name: "Avenir", size: 20)!
    self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: navigationTitleFont]

    self.navigationController?.navigationBar.translucent = true

    self.searchController = ({
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.hidesNavigationBarDuringPresentation = true
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.sizeToFit()
        self.tableView.tableHeaderView = controller.searchBar
        self.definesPresentationContext = true
        controller.searchBar.returnKeyType = .Search
        controller.searchBar.delegate = self

        return controller

    })()

    searchController.searchBar.placeholder = "Search Employees"
    for subView in searchController.searchBar.subviews {
        for subsubView in subView.subviews {
            if let textField = subsubView as? UITextField {
                textField.attributedPlaceholder = NSAttributedString(string: "Search Employees", attributes: [NSFontAttributeName: UIFont(name: "Avenir", size: 14)!])
                textField.font = UIFont(name: "Avenir", size: 14)
            }
        }
    }

    searchController.searchBar.setBackgroundImage(UIImage(named: "blue"), forBarPosition: .Any, barMetrics: .Default)
    searchController.searchBar.backgroundColor = UIColor.clearColor()
    searchController.searchBar.barTintColor = UIColor(red: 41/255, green: 128/255, blue: 185/255, alpha: 1.0)
    searchController.searchBar.tintColor = UIColor.whiteColor()
    searchController.searchBar.clipsToBounds = true

2 个答案:

答案 0 :(得分:1)

您可以通过将表视图的顶部内容插入设置为-1来解决此问题:

self.tableView.contentInset = UIEdgeInsetsMake(-1, 0, 0, 0)

答案 1 :(得分:1)

尝试这一点,我不做swift,所以这可能是语法上的,不正确的,但它确实在swift代码中编译,这是从ObjC翻译的,我总是这样做:

    self.navigationController?.navigationBar.setBackgroundImage(UIImage .new(), forBarMetrics: .Default)
    self.navigationController?.navigationBar.shadowImage = UIImage .new()
    self.navigationController?.navigationBar.layer.shadowRadius = 0
    self.navigationController?.navigationBar.layer.shadowOpacity = 0
    self.navigationController?.navigationBar.layer.shadowOffset = CGSizeMake(0, 0)

    self.navigationController?.navigationBar.translucent = true
    self.navigationController?.navigationBar.backgroundColor = UIColor .clearColor()
    self.navigationController?.navigationBar.tintColor = UIColor .clearColor()

这样的事情也可能有所帮助:

var sds: UISearchBar

        sds.layer.borderColor = UIColor .clearColor().CGColor
        sds.layer.borderWidth = 1