2016-11-21 101 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; 
      } 
     } 
    } 
}