Remove border between View and Search Bar
Asked Answered
A

8

23

So in Xcode I'm trying to create a seamless search bar. So I'm trying to replicate something like this

enter image description here

Note how the status bar is the same color as the search bar. Now here's the result to my approach.

enter image description here

What I did was add a View to cover up the default status bar with the blue background. Then I added a search bar and changed it's background to blue. For some reason I end up getting a black border between the two, which ruins the "seamless" design. Any ideas on how I can remove the black border in Swift?

Achaean answered 25/7, 2015 at 22:1 Comment(1)
that seems to work: #6868714 has been tried by this user: jslim.net/blog/2014/07/14/…Wharf
C
54

For iOS 7+:

searchBar.backgroundImage = UIImage()

Otherwise this will work on all iOS versions:

searchBar.layer.borderWidth = 1
searchBar.layer.borderColor = thatBlueColor.CGColor
Coefficient answered 25/7, 2015 at 22:16 Comment(8)
The otherwise part should work for iOS 7+ too, according to this source: jslim.net/blog/2014/07/14/…Wharf
Yeah you're right. However, I prefer the top one. It's cleaner and doesn't require you to have the color on hand.Coefficient
Just did this but I still get the border. Also when doing the first one I get an error: Cannot invoke 'setBackgroundImage' with an argument list of type '(UIImage)'Achaean
@Achaean whoops. Try that.Coefficient
I then end up getting Invalid use of '()' to call a value of non-function type 'CGColor'. I'm using Swift 2, if that makes a difference...Achaean
Also I just gave the first one a shot, which doesn't toss an error, but it returns a white search bar, instead of the desired color via interface builder.Achaean
Try the second one again. I couldn't remember if cgcolor was converted to a property as opposed to a function.Coefficient
Top one does not work on ipad or ipod. Only the bottom one.Carmel
S
8

Swift 4

searchBar.barTintColor = UIColor.white
searchBar.setBackgroundImage(UIImage.init(), for: UIBarPosition.any, barMetrics: UIBarMetrics.default)

Sample image

enter image description here

Upate Sample code for navigation bar and search bar background color:

Navigation bar color

self.navigationController?.navigationBar.barTintColor = .blue

Search bar color

searchBarProperty.backgroundColor = self.navigationController?.navigationBar.barTintColor

Note : Navigation bar and search bar color must be same.

Sample image with blue navigation bar and blue search bar enter image description here

Shew answered 4/7, 2018 at 5:18 Comment(3)
this only seems to work for a white navigation bar. i'm trying it with a dark blue one and it still comes out white like in yours (and yes i have changed the barTintColor parameter)Trichinopoly
You should check navigation bar and search bar color,If both color are not same then you need to change navigation bar color and searchbar color, Please check my updated answerShew
yeah i got it working after like 8 hours... thanks anyways. upvoted yesterdayTrichinopoly
B
6

In Xcode 8.3 and Swift 3

  1. Create an outlet from your search bar to your ViewController (I called mine searchBarOutlet for this example).

  2. Below viewDidLoad insert the following.

    self.searchBarOutlet.backgroundImage = UIImage()

You should have the following:

    override func viewDidLoad() {
    super.viewDidLoad()

         self.searchBarOutlet.backgroundImage = UIImage()

When you run your app the lines will be gone (they will still be visible on storyboard).

Braun answered 16/8, 2017 at 12:41 Comment(0)
T
4

In my case, beyond the edge of search bar needed to take the edge off also the navigation bar.

C# code:

NavigationController.NavigationBar.ShadowImage = new UIImage();
NavigationController.NavigationBar.SetBackgroundImage (new UIImage (), UIBarMetrics.Default);

Swift code:

self.navigationController.navigationBar.shadowImage = UIImage()
self.navigationController.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
Towrope answered 3/9, 2015 at 12:44 Comment(1)
The question was Swift related, not C#.Foresaid
B
4

in Xcode 13

select the search bar and change the search Style to Minimal

enter image description here

Boomer answered 23/10, 2022 at 20:16 Comment(0)
T
2

The best solution to remove top and bottom default borders is:

To set a new empty searchBar background layout in viewDidLoad for example:

searchBar.backgroundImage = UIImage()
Thyme answered 17/4, 2017 at 20:58 Comment(0)
I
1

I found these answers to be more complicated than they needed to be. You can just modify the constraint that is binding the searchBar view and the other view to -1pts so that it overlaps exactly by the height of the searchBar's margin.

Independent answered 13/10, 2016 at 15:43 Comment(0)
P
1

I encountered the same situation when I set the statusBar and searchBar translucent. In this situation, I couldn't resolve with the answers written here however I could solve by the following approach.

  • put UIVisualEffectView on self.view (view of your VC)
  • make custom class of searchBar, which background is transparent
  • (also let statusBar transparent)

(swift4 code)

    class TransparentSearchBar: UISearchBar {
        override init(frame: CGRect) {
            super.init(frame: frame)
        }

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }

        override func layoutSubviews() {
            super.layoutSubviews()
            makeTransparentBackground()
        }

        private func makeTransparentBackground() {
            for view in self.subviews {
                view.backgroundColor = UIColor.clear
                for subview in view.subviews {
                    if let imageview = subview as? UIImageView {
                        imageview.image = nil
                    }
                }
            }
        }

    }

somewhere in viewDidLoad (statusBar)

    let statusWindow = UIApplication.shared.value(forKey:"statusBarWindow") as! UIView
    let statusBar = statusWindow.subviews[0] as UIView
    statusBar.backgroundColor = UIColor.clear
Pemmican answered 16/11, 2018 at 9:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.