Rehaan Advani
Rehaan Advani

Reputation: 975

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

enter image description here

How do I get rid of this small gap between the navigation bar and the search bar?

Here is the code for customizing the appearance of the navigationbar and the search bar:

 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

Upvotes: 1

Views: 1153

Answers (2)

Larry Pickles
Larry Pickles

Reputation: 4646

Try this out, I don't do swift, so this could be syntactically, incorrect, but it does compile in swift code, this is translated from ObjC where I do this sort of thing all the time:

    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()

Something like this may help as well:

var sds: UISearchBar

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

Upvotes: 1

Palle
Palle

Reputation: 12129

You can solve this by setting the top content inset of your table view to -1:

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

Upvotes: 1

Related Questions