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

How to get rid of small gap between UINavigationController and UISearchBar?

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

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

 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

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

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

试试这个,我不做 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