43

UISearchBar 1px black border

I'm having trouble removing that 1px border directly under the UISearchBar view. Now it doesn't look matched up with my done button view. I've tried:

searchBar.layer.borderWidth = 0;
searchBar.layer.shadowOpacity = 0;

But that doesn't seem to be working. Any ideas?

1
  • Perhaps it's part of the design?
    – apscience
    Jul 29, 2011 at 3:04

4 Answers 4

94

Nevermind, I just did:

searchBar.layer.borderWidth = 1;
searchBar.layer.borderColor = [[UIColor whiteColor] CGColor];

and it works!

6
  • 1
    this doesn't work for me. I don't get any options when I try autocomplete after searchBar.layer. Any ideas why?
    – budiDino
    Feb 4, 2012 at 17:05
  • 3
    OK, I had to: #import <QuartzCore/QuartzCore.h>
    – budiDino
    Feb 4, 2012 at 17:26
  • The only reason why it worked is because you set the border color to your view's background color. But it's the best solution I found so far.
    – testing
    Nov 6, 2014 at 9:35
  • Works for me, iOS 8! Thanks! Nice idea! =)
    – Nubaslon
    Jan 9, 2015 at 13:57
  • 2
    for matching background color, searchBar.layer.borderColor = searchBar.barTintColor.CGColor;
    – bisma
    Feb 25, 2016 at 10:38
52
[searchBar setBackgroundImage:[UIImage new]];
2
  • 1
    I think this is a iOS 7+ solution, which is what I needed. Thanks!
    – rob5408
    Sep 14, 2014 at 21:26
  • This depends on the background color you are using.
    – testing
    Nov 6, 2014 at 9:36
23

For Swift version, tested on iOS9:

searchBar.backgroundImage = UIImage() 

It would show like this:

no-border-result

2
  • I spend almost 4 hours. Finally, I found a perfect solution. Thank you.
    – Umair_UAS
    Dec 17, 2019 at 17:46
  • still works on ios 15 and swift 5
    – Underdog
    Nov 18, 2021 at 5:52
4

In order to overcome this in Xamarin Forms, you'll need to create a CustomRenderer to the SearchBar class. Like this:

using System;
using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms;
using MyProject.iOS;

[assembly: ExportRenderer(typeof(SearchBar), typeof(CustomSearchBarRenderer))]
namespace MyProject.iOS
{
    public class CustomSearchBarRenderer:SearchBarRenderer
    {
        protected override void OnElementChanged (ElementChangedEventArgs<Xamarin.Forms.SearchBar> e)
        {
            base.OnElementChanged (e);
            if (this.Control == null) return;

            this.Control.BackgroundImage = new UIKit.UIImage ();
        }
    }
}
1
  • 2
    Could you explain how this works and why the more simple answers aren't good enough?
    – meneldal
    Dec 20, 2015 at 23:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.