2015-12-29 69 views
1

有很多關於如何在UISearchbar中訪問UITextField的Objective-C變體,因此您可以比暴露的SearchBar方法更具體地設計它的樣式,即我們想要更改 -設置Xamarin UISearchBar UITextField字體大小/顏色+背景

  • 字體類型/大小
  • 字體顏色
  • 文本字段背景的顏色,以便搜索欄是一種顏色

認爲這將是很高興看到Xamar在溶液(個),這是一定的OBJ-C技術似乎並沒有(我可能只是缺少代碼)翻譯 -

例如,在 -

Change the font size and font style of UISearchBar iOS 7

- (void)viewDidLoad 
{ 

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{ 
     NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:20], 
    }]; 

} 

外貌像一個勝利者,但性質不是「看似」暴露不知道能翻譯這Xamarin -

UITextField.AppearanceWhenContainedIn(typeof (UISearchBar))).HowToSetFont ... 

我會首發回答一個我剛纔碰到過,BU沒有興趣瞭解其他用戶如何解決樣式搜索欄的問題。

回答

3
_filterSearchBar = new UISearchBar 
{ 
    BarTintColor = UIColor.Red,  // We want a red search bar with text field background same as outer "margin" 
    TintColor = UIColor.White, 
    SearchBarStyle = UISearchBarStyle.Default, 
    BackgroundImage = UIImage.FromFile("transparent1x1.png")  // "trick" to ensure background is clean 
}; 

// Main Part - Use KVO to access text field 
var uiTextField = (UITextField)_filterSearchBar.ValueForKey(new NSString("_searchField")); 

uiTextField.Font = YourFontHere; 
uiTextField.TextColor = UIColor.White; 
uiTextField.BackgroundColor = UIColor.Red;    

// Placeholder colour is white 
uiTextField.AttributedPlaceholder = new NSAttributedString("SearchBarPlaceholder", null, UIColor.White); 

// We still have not sorted spyglass icon colour et al to white but can try set icon image later