Ios 如何消除UINavigationController和UISearchBar之间的小间隙?

Ios 如何消除UINavigationController和UISearchBar之间的小间隙?,ios,swift,uinavigationbar,uisearchbar,uisearchcontroller,Ios,Swift,Uinavigationbar,Uisearchbar,Uisearchcontroller,如何消除导航栏和搜索栏之间的这个小间隙 以下是用于自定义导航栏和搜索栏外观的代码: self.navigationController?.navigationBar.barStyle = .Black self.navigationController?.navigationBar.barTintColor = UIColor(red: 41/255, green: 128/255, blue: 185/255, alpha: 1.0) self.navigationContr

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

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

 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

您可以通过将表视图的顶部内容插入设置为-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

这个小缺口会是UINavigationBar的边界吗?如果是这样的话,也许可以帮你。不,这似乎不是边界。我添加了这行代码:self.navigationController?.navigationBar.layer.borderWidth=0。这没有任何作用。UINavigationBar上的边框不是用CALayer制作的,而是用UIImageView制作的。答案,我在第一条评论中添加了一个链接,显示了一种摆脱它的方法。@blobbfuesch我看了你提供的答案,它似乎不起作用。我设置了导航栏的背景图像和阴影图像,但不起作用。为什么要使用
controller.searchBar.sizeToFit()
searchController.searchBar.clipsToBounds=true
?我认为您的样式做得太多了,并且可以用更少的代码行实现所需的效果。逐个注释掉所有的外观代码和取消注释行,查看问题的原因。感谢您的回复!我实现了这个,但它不起作用。还有其他想法吗?谢谢你的回复!我试过这个,但它只是让我的导航栏有一个白色的背景色。还有其他想法吗?将背景色设置为“清晰颜色”,这可能会起作用,然后将其设置为“半透明”,因为这将删除整个导航栏,基本上,除了导航项,您可能需要将导航控制器视图的背景颜色设置为您想要使用的蓝色。这使我的导航栏和状态栏完全透明。但是,它是否仍有导航项?