Xamarin.Forms SearchBar의 스타일을 지정하려고하는데 BackgroundColor 속성이 있음을 알 수 있습니다. 그러나 설정 한 내용과 상관없이 속성은 iOS에서 무시됩니다.iOS에서 Xamarin.Forms 검색 표시 줄을 어떻게 스타일링 할 수 있습니까?
iOS (및 방법)에서 Xamarin.Forms SearchBar를 사용자 정의 할 수 있습니까?
Xamarin.Forms SearchBar의 스타일을 지정하려고하는데 BackgroundColor 속성이 있음을 알 수 있습니다. 그러나 설정 한 내용과 상관없이 속성은 iOS에서 무시됩니다.iOS에서 Xamarin.Forms 검색 표시 줄을 어떻게 스타일링 할 수 있습니까?
iOS (및 방법)에서 Xamarin.Forms SearchBar를 사용자 정의 할 수 있습니까?
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);
}
}
}
코드는 다소 거칠지 만 잘하면 아이디어를 얻을 수 있습니다.
추가 참고 사항 몇이의
불행히도 답변을 받아 들일 수 없습니다. 아래 코드는 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;
}
}
}
OnElementChanged를 재정의하면이 문제를 해결하는 올바른 방법입니다. 좋은 대답! – Blounty
Xam Forms를 통해 수행 할 방법이없는 경우 사용자 지정 렌더러를 만들 수 있습니다. –
그럴 수도 있습니다. 이것은 Android에서 작동하며 UISearchBar 컨트롤에 배경색 속성 (Bar Tint)이 있다는 것을 알고 있으므로 iOS에서도 작동해야하는 것처럼 보입니다. –
derek이 말했듯이 다음 중 하나를 만들 수 있습니다. http://developer.xamarin.com/guides/cross-platform/xamarin-forms/custom-renderer/ –