2016-11-21 96 views
1

我想在我的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; 
} 
} 

在此先感谢

回答

0

我想我设法与手动删除它:

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

编写代码隐藏在layoutsubviews方法取消按钮。

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

      } 

下面的内容也是工作还是我,没有需要继承搜索:

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

      }; 
+0

不幸运!已经尝试过 – user3421325

1

这为我工作。 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; 
      } 
     } 
    } 
}