2014-06-24 5 views
1

Xamarin.Forms SearchBar의 스타일을 지정하려고하는데 BackgroundColor 속성이 있음을 알 수 있습니다. 그러나 설정 한 내용과 상관없이 속성은 iOS에서 무시됩니다.iOS에서 Xamarin.Forms 검색 표시 줄을 어떻게 스타일링 할 수 있습니까?

iOS (및 방법)에서 Xamarin.Forms SearchBar를 사용자 정의 할 수 있습니까?

+1

Xam Forms를 통해 수행 할 방법이없는 경우 사용자 지정 렌더러를 만들 수 있습니다. –

+0

그럴 수도 있습니다. 이것은 Android에서 작동하며 UISearchBar 컨트롤에 배경색 속성 (Bar Tint)이 있다는 것을 알고 있으므로 iOS에서도 작동해야하는 것처럼 보입니다. –

+1

derek이 말했듯이 다음 중 하나를 만들 수 있습니다. http://developer.xamarin.com/guides/cross-platform/xamarin-forms/custom-renderer/ –

답변

4

Xamarin.Forms가 BackgroundColor 속성을 구현하지 못하거나 깨졌습니다. 실제 배경색과 가장 가까운 UISearchBar 속성은 BarTint이며 XForms에서는 설정하지 않습니다.

이 문제를 해결하기 위해 저는 의견을 마음에 새기고 사용자 지정 렌더러가있는 사용자 정의 검색 모음을 만들어 BarTint 속성과 원하는 다른 몇 가지 항목을 확장했습니다.

참고 : 사용자 지정 렌더러를 사용하려면 Xamarin.Forms 1.1.1.6206 이상으로 업데이트해야합니다. 플랫폼 버전을 업데이트하려면 Visual Studio에서 NuGet을 사용하거나 XamarinStudio에 내장 된 패키지 관리자를 사용하십시오.

두 개의 클래스가 필요하며 첫 번째 클래스는 사용자 지정 속성을 유지하는 데 사용할 CustomSearchBar입니다. 공유 또는 이식 가능한 클래스 라이브러리에 저장됩니다. :

using Xamarin.Forms; 

namespace App1 
{ 
    public class CustomSearchBar : SearchBar 
    { 
     // Use Bindable properties to maintain XAML binding compatibility 

     public static readonly BindableProperty BarTintProperty = BindableProperty.Create<CustomSearchBar, Color?>(p => p.BarTint, null); 
     public Color? BarTint 
     { 
      get { return (Color?)GetValue(BarTintProperty); } 
      set { SetValue(BarTintProperty, value); } 
     } 

     public static readonly BindableProperty SearchStyleProperty = BindableProperty.Create<CustomSearchBar, string>(p => p.SearchStyle, "Default"); 
     public string SearchStyle 
     { 
      get { return (string)GetValue(SearchStyleProperty); } 
      set { SetValue(SearchStyleProperty, value); } 
     } 

     public static readonly BindableProperty BarStyleProperty = BindableProperty.Create<CustomSearchBar, string>(p => p.BarStyle, "Default"); 
     public string BarStyle 
     { 
      get { return (string)GetValue(BarStyleProperty); } 
      set { SetValue(BarStyleProperty, value); } 
     } 

    } 
} 

두 번째 클래스는 기본 UISearchButton 컨트롤에 액세스 할 수있는 사용자 지정 렌더러입니다. 이 수업은 iOS 프로젝트에 적용됩니다.

using System; 
using System.Drawing; 
using App1; 
using App1.iOS; 
using MonoTouch.UIKit; 
using Xamarin.Forms; 
using Xamarin.Forms.Platform.iOS; 

[assembly: ExportRendererAttribute(typeof(CustomSearchBar), typeof(CustomSearchBarRenderer))] 
namespace App1.iOS 
{ 
    public class CustomSearchBarRenderer : SearchBarRenderer 
    { 
     // There might be a better place for this, but I don't know where it is 
     public override void Draw(RectangleF rect) 
     { 
      var csb = (CustomSearchBar) Element; 
      if (csb.BarTint != null) 
       Control.BarTintColor = csb.BarTint.GetValueOrDefault().ToUIColor(); 
      Control.BarStyle = (UIBarStyle)Enum.Parse(typeof(UIBarStyle), csb.BarStyle); 
      Control.SearchBarStyle = (UISearchBarStyle)Enum.Parse(typeof(UISearchBarStyle), csb.BarStyle); 

      base.Draw(rect); 
     } 
    } 
} 

코드는 다소 거칠지 만 잘하면 아이디어를 얻을 수 있습니다.

추가 참고 사항 몇이의

  • 없음이 ExportRendererAttribute없이 작동합니다, 그래서 은을 두지 마십시오.
  • BarTint를 nullable로 설정해야했습니다 (기본값은 이고 Color.Default는 검정색이므로 null 임).
  • 코드를 넣을 곳이 어디인지 알 수 없기 때문에 기술적으로 그리지 않고 있어도 Draw()를 재정의합니다. 컨트롤 및 요소 속성은 생성자에서 사용할 수 없습니다. 더 나은 해결책을 찾으면이 예제를 업데이트하려고 노력할 것입니다.
  • 문자열을 Enum 속성으로 사용하지만 향후에이를 향상시킬 수 있습니다.
4

불행히도 답변을 받아 들일 수 없습니다. 아래 코드는 iOS에서 작동합니다. Draw가 아닌 ​​OnElementChanged가 이러한 유형의 것을 사용자 정의 렌더러에 배치하는 데 선호되는 것으로 보입니다.

using MonoTouch.UIKit; 

using Xamarin.Forms; 
using Xamarin.Forms.Platform.iOS; 

[assembly:ExportRenderer(typeof(MyNamespace.MySearchBar), typeof(MyNamespace.iOS.MySearchBarRenderer_iOS))] 


namespace MyNamespace.iOS 
{ 
    public class MySearchBarRenderer_iOS : 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.Green; 
      bar.KeyboardType = UIKeyboardType.ASCIICapable; 
     } 
    } 
} 
+0

OnElementChanged를 재정의하면이 문제를 해결하는 올바른 방법입니다. 좋은 대답! – Blounty

관련 문제