有很多关于如何在UISearchbar中访问UITextField的Objective-c变体,因此您可以比暴露的SearchBar方法更具体地设置样式,即我们想要更改 -
认为看到Xamarin解决方案很好,因为某些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 ...
我会为我刚刚遇到的一个做出初步答案,但有兴趣了解其他用户如何解决搜索栏的样式。
答案 0 :(得分:4)
_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