【问题标题】:Remove the 1px border under UISearchBar去掉 UISearchBar 下的 1px 边框
【发布时间】:2011-07-29 03:00:39
【问题描述】:

我无法直接移除 UISearchBar 视图下的 1px 边框。现在它看起来与我的完成按钮视图不匹配。我试过了:

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

但这似乎不起作用。有什么想法吗?

【问题讨论】:

  • 也许这是设计的一部分?

标签: objective-c xcode uiview uikit uisearchbar


【解决方案1】:

没关系,我刚刚做了:

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

它有效!

【讨论】:

  • 这对我不起作用。当我在 searchBar.layer 之后尝试自动完成时,我没有任何选项。任何想法为什么?
  • 好的,我必须:#import
  • 它起作用的唯一原因是您将边框颜色设置为视图的背景颜色。但这是迄今为止我找到的最好的解决方案。
  • 适用于我,iOS 8!谢谢!好主意! =)
  • 为了匹配背景颜色,searchBar.layer.borderColor = searchBar.barTintColor.CGColor;
【解决方案2】:
[searchBar setBackgroundImage:[UIImage new]];

【讨论】:

  • 我认为这是一个 iOS 7+ 的解决方案,这正是我所需要的。谢谢!
  • 这取决于您使用的背景颜色。
【解决方案3】:

Swift版本,在iOS9上测试:

searchBar.backgroundImage = UIImage() 

它会显示如下:

【讨论】:

  • 我花了将近 4 个小时。最后,我找到了一个完美的解决方案。谢谢。
  • 仍然适用于 ios 15 和 swift 5
【解决方案4】:

为了在 Xamarin Forms 中克服这个问题,您需要为 SearchBar 类创建一个 CustomRenderer。 像这样:

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

【讨论】:

  • 您能否解释一下这是如何工作的以及为什么更简单的答案不够好?
猜你喜欢
  • 2011-01-24
  • 2014-08-05
  • 2019-11-28
  • 2011-01-17
  • 1970-01-01
  • 2020-11-01
  • 2012-11-22
  • 2010-09-19
相关资源
最近更新 更多