【问题标题】:How to get rid of small gap between UINavigationController and UISearchBar?如何摆脱 UINavigationController 和 UISearchBar 之间的小差距?
【发布时间】:2015-08-15 01:34:53
【问题描述】:

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

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

 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

【问题讨论】:

  • 这个小间隙可以是 UINavigationBar 的边框吗?如果是这样,也许this answer 可以帮助你。
  • 不,好像不是边界。我添加了这行代码:self.navigationController?.navigationBar.layer.borderWidth = 0。这什么也没做。
  • UINavigationBar 上的边框不是用 CALayer 做的,而是用 UIImageView 做的。答案,我在第一条评论中放了一个链接,显示了一种摆脱它的方法。
  • @blobbfuesch 我查看了您提供的答案,但似乎没有用。我设置了导航栏的背景图和阴影图,但是没用。
  • 你为什么使用controller.searchBar.sizeToFit()searchController.searchBar.clipsToBounds = true?我相信您是过度使用样式,并且可以用更少的代码行来实现所需的效果。把所有的外观代码注释掉,把注释行一一取消,看看是什么原因造成的。

标签: ios swift uinavigationbar uisearchbar uisearchcontroller


【解决方案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

【讨论】:

  • 感谢您的回复!我试过这个,但它只是让我的导航栏有一个白色的背景颜色。还有其他想法吗?
  • 将背景颜色设置为透明色,这可能会起作用,然后将其设置为半透明
  • 另外,由于这将删除整个导航栏,基本上,除了导航项,您可能需要将导航控制器视图的背景颜色设置为您想要的蓝色颜色使用
  • 这只是让我的导航栏和状态栏完全透明
  • 但是,它还有导航项吗?
【解决方案2】:

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

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

【讨论】:

  • 感谢您的回复!我实现了这一点,但没有奏效。还有其他想法吗?