简体   繁体   English

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

[英]Xamarin iOS Hide cancel button from Search Bar

I wanted to hide 'cancel' button in my iOS search bar.我想在我的 iOS 搜索栏中隐藏“取消”按钮。 I have implemented the following custom renderer code but it seems not to to work.我已经实现了以下自定义渲染器代码,但它似乎不起作用。 If anyone knows solution, please share.如果有人知道解决方案,请分享。

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

Thanks in advance提前致谢

This worked for me. 这对我有用。 https://gist.github.com/xleon/9f94a8482162460ceaf9 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;
            }
        }
    }
}

I think i managed to remove it manually with: 我想我设法用以下手动删除它:

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

Write code to hide cancel button in layoutsubviews method. 编写代码以隐藏layoutsubviews方法中的取消按钮。

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

            }

Following is also working or me, no need to subclass searcher: 以下也工作或我,不需要子类搜索者:

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

            };

I spend some more time searching for this, so adding here in case someone else wants to do both.我花了更多时间搜索这个,所以在这里添加以防其他人想要同时做这两个事情。 In case you also need to remove the X button as well, I found the solution in this comment如果您还需要删除 X 按钮,我在此评论中找到了解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM