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

use*_*325 4 uisearchbar xamarin.ios ios xamarin

我想在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;
}
}
Run Code Online (Sandbox Code Playgroud)

提前致谢

小智 5

这对我有用。 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;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Ori*_*ice 0

我想我设法手动删除它:

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