Remove the 1px border under UISearchBar

2020-02-19 02:49发布

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?

4条回答
孤傲高冷的网名
2楼-- · 2020-02-19 03:25

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 ();
        }
    }
}
查看更多
戒情不戒烟
3楼-- · 2020-02-19 03:27

Nevermind, I just did:

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

and it works!

查看更多
太酷不给撩
4楼-- · 2020-02-19 03:37
[searchBar setBackgroundImage:[UIImage new]];
查看更多
Melony?
5楼-- · 2020-02-19 03:48

For Swift version, tested on iOS9:

searchBar.backgroundImage = UIImage() 

It would show like this:

no-border-result

查看更多
登录 后发表回答