Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove top and bottom border of UISearchBar and shadow?

Tags:

ios

swift

I have created a .xib with a UISearchBar.

I am looking to remove the top and bottom borders as seen in this image: image

There is also what looks like a shadow underneath the top border.

I have tried changing backgrounds/tint colours etc, but struggling to get it to disappear.

Any help would be welcome, any ideas?

EDIT

So the code below allows me to get rid of the border at the top and bottom, but not the weird drop shadow. I've added green background to show it more clearly.

See

Also, what would be the best way to add a border around the text box (rather than the view)?? Thanks

//** This is what I'm trying to do: https://stackoverflow.com/questions/33107058/uiviewcontroller-sizing-as-maincontroller-within-uisplitviewcontroller

and here is the code I am using.

import UIKit

class TopSearchViewController: UIView {

    var expanded: Int = 0

    @IBOutlet weak var trailingConstraint: NSLayoutConstraint!
    @IBOutlet var contentView: UIView!
    @IBOutlet weak var searchBar: UISearchBar!

    @IBAction func advancedSearchButton(sender: AnyObject) {
        if expanded == 0 {
            self.layoutIfNeeded()
            UIView.animateWithDuration(0.1, animations: {
                self.trailingConstraint.constant = 200
                self.layoutIfNeeded()
            })
            expanded = 1
        } else {
            self.layoutIfNeeded()
            UIView.animateWithDuration(0.1, animations: {
                self.trailingConstraint.constant = 25
                self.layoutIfNeeded()
            })
            expanded = 0
        }
    }

    override init(frame: CGRect) { // for using CustomView in code
        super.init(frame: frame)
        self.commonInit()
    }

    required init(coder aDecoder: NSCoder) { // for using CustomView in IB
        super.init(coder: aDecoder)
        self.commonInit()
    }

    private func commonInit() {
        NSBundle.mainBundle().loadNibNamed("TopSearchViewController", owner: self, options: nil)

        contentView.frame = self.bounds
        contentView.autoresizingMask = .FlexibleHeight | .FlexibleWidth

        self.addSubview(contentView)

        self.searchBar.layer.borderWidth = 1
        self.searchBar.layer.borderColor = UIColor.whiteColor().CGColor

        println(searchBar)

        for subview in searchBar.subviews {
            println("hello")

            var textField : UITextField

            if (subview.isKindOfClass(UITextField)) {
                textField = subview as! UITextField
                textField.borderStyle = .None
                textField.layer.borderWidth = 1
                textField.layer.borderColor = UIColor.lightGrayColor().CGColor
                textField.layer.cornerRadius = 14
                textField.background = nil;
                textField.backgroundColor = UIColor.whiteColor()
            }
        }

    }

    func viewDidLoad() {

    }
}
like image 855
Brian Marsh Avatar asked Oct 15 '15 11:10

Brian Marsh


2 Answers

Set the bar's backgroundImage to an empty UIImage, not nil:

searchBar.backgroundImage = UIImage()

You may also set the barTintColor, as you specified in your comment below.

like image 168
NRitH Avatar answered Nov 12 '22 00:11

NRitH


With Xcode 11, in the Storyboard, you can set 'Search Style' to 'Minimal.'

In code: searchBar.searchBarStyle = .minimal

like image 24
andrewlundy Avatar answered Nov 11 '22 23:11

andrewlundy