Xamarin iOS隐藏搜索栏中的取消按钮

时间:2016-11-21 17:25:27

标签: ios xamarin xamarin.ios uisearchbar

我想隐藏'取消'我的iOS搜索栏中的按钮。我已经实现了以下自定义渲染器代码,但它似乎无法工作。如果有人知道解决方案,请分享。

public class iOSSearchBar : SearchBarRenderer 
    {
        protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> args)
        {
            base.OnElementChanged(args);

        UISearchBar bar = (UISearchBar)this.Control;

        bar.AutocapitalizationType = UITextAutocapitalizationType.None;
        bar.AutocorrectionType = UITextAutocorrectionType.No;
        //bar.BarStyle = UIBarStyle.Default;
        //bar.BarTintColor = UIColor.LightGray;
        //bar.KeyboardType = UIKeyboardType.ASCIICapable;
        bar.SearchBarStyle = UISearchBarStyle.Minimal;
        bar.SetShowsCancelButton(false, false);
        bar.ShowsCancelButton = false;
}
}

提前致谢

3 个答案:

答案 0 :(得分:2)

这对我有用。 https://gist.github.com/xleon/9f94a8482162460ceaf9

using System;
using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms;
using UIKit;
using System.Diagnostics;

[assembly: ExportRenderer(typeof(SearchBar), typeof(Namespace.iOS.Renderers.ExtendedSearchBarRenderer))]
namespace Namespace.iOS.Renderers
{
    public class ExtendedSearchBarRenderer : SearchBarRenderer
    {
        protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            if (e.PropertyName == "Text")
            {
                Control.ShowsCancelButton = false;
            }
        }
    }
}

答案 1 :(得分:0)

我想我设法通过以下方式手动删除它:

 protected override void OnElementChanged(ElementChangedEventArgs<SearchBar> e)
    {
        base.OnElementChanged(e);
        if (Control != null)
        {
            Control.Subviews[0].Subviews[0].RemoveFromSuperview();          
        }
    }

答案 2 :(得分:0)

编写代码以隐藏layoutsubviews方法中的取消按钮。

  public override void LayoutSubviews()
            {
                base.LayoutSubviews();
                UISearchBar bar = (UISearchBar)this.Control;
                bar.ShowsCancelButton = false;

            }

以下也工作或我,不需要子类搜索者:

SearchBar.TextChanged += delegate
            {
                SearchBar.ShowsCancelButton = false;

            };