【问题标题】:How to remove the border or border colour of the SearchBar?如何去除搜索栏的边框或边框颜色?
【发布时间】:2016-04-21 12:58:25
【问题描述】:

我是 Xamarin.forms 的新手。我使用的是 SearchBar,然后是 BoxView

我面临的问题是删除 SearchBar 的边界线。

这个边框让 UI 看起来好像 SearchBar 和 BoxView 之间有一个分隔符。

非常感谢任何帮助实现这一目标。

非常感谢:)

【问题讨论】:

  • 您在哪个平台上看到这个?你能包括你的 Xaml
  • 看起来灰色分隔符是您的 BoxView。你能说得更具体点吗?
  • @MarioGalván 它不是 BoxView 的边框。因为,BoxView 的底部没有显示相同的边框。它只是 SearchBar 的边界。

标签: xamarin xamarin.ios xamarin.android xamarin.forms xamarin-studio


【解决方案1】:

认为您可能需要一个自定义渲染器,如下所示:

using MonoTouch.UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly:ExportRenderer( typeof(NamespaceOfApp.MySearchBar), typeof(NamespaceOfApp.iOS.SearchBarWithNoBarRenderer_iOS))]

namespace NamespaceOfApp.iOS
{
    public class SearchBarWithNoBarRenderer_iOS : SearchBarRenderer
    {
        protected override void OnElementChanged( ElementChangedEventArgs<SearchBar> args )
        {
            base.OnElementChanged( args );

            UISearchBar bar = (UISearchBar)this.Control;
            //set background to empty image
            bar.SetBackgroundImage (new UIImage (), UIBarPosition.TopAttached, UIBarMetrics.Default);
        }
    }
}

如果您需要自定义渲染器,请查看 here

【讨论】:

  • 太棒了!!!! @LainSmith ...该建议非常值得赞赏...但是我正在通过不使用渲染器来寻找解决方案。如果这是唯一的选择,我会去的。
【解决方案2】:

在 Forms v2 中引入了效果,作为扩展 Forms 基本控件的推荐方式。 https://developer.xamarin.com/guides/xamarin-forms/effects/

例如;这将是您的 iOS 效果代码...

using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly:ResolutionGroupName ("MyCompany")]
[assembly:ExportEffect (typeof(FocusEffect), "FocusEffect")]
namespace EffectsDemo.iOS
{
    public class FocusEffect : PlatformEffect
    {
        protected override void OnAttached ()
        {
            // your code here        
        }

        protected override void OnDetached ()
        {
            // your code here        
        }

        protected override void OnElementPropertyChanged (PropertyChangedEventArgs args)
        {
            base.OnElementPropertyChanged (args);
            // your code here        
        }
    }
}

自定义渲染器是 Forms v1.x 中的唯一选项。自定义渲染器非常适合从头开始构建控件,但在扩展内置控件时它有点失败。

【讨论】:

    【解决方案3】:

    使用故事板:

    使用代码:

    public override void ViewWillLayoutSubviews ()
            {
                base.ViewWillLayoutSubviews ();
                sampleSearchBar.SearchBarStyle = UISearchBarStyle.Minimal;
                sampleSearchBar.BarTintColor =  UIColor.Clear;
    
            }
    

    【讨论】:

    • 很好!! @Alvin George ......截至目前,我正在设计将在 iOS 和 Android 上共享的 UI。如果我使用 Storyboard 并且 UI 仅适用于 iOS 设备,我相信您的回答可能会对我有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    • 1970-01-01
    • 2017-09-30
    相关资源
    最近更新 更多