ios - 更改以编程方式创建的 UISearchBar 的字体

标签 ios uisearchbar

我发现我的 AppDelegate 中的代码:

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil]
 setFont:[UIFont systemFontOfSize:14]];

用于更改从 IB 创建的 UISearchBar 的字体。那里没有问题。

但是,如果我在这样的代码中创建 UISearchBar:

UISearchBar *bar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
bar.placeholder = @"test";
[self.view addSubview:bar];

那么上面的UITextField外观代码就没有作用了。有没有办法更改以编程方式创建的 UISearchBar 的字体?

谢谢!

最佳答案

试试这个,它适用于 iOS 5.0 及更高版本:(也适用于 iOS 7)

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:@"League Gothic" size:20]];

关于ios - 更改以编程方式创建的 UISearchBar 的字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18070549/

相关文章:

iOS 11.0 推送 View Controller 与 navigationItem.searchController 的问题

iphone - 增加 TableView 标题的大小

objective-c - IOS:在表格 View 中搜索名称

objective-c - ASIHttp 框架的 Apple Mach-O 链接器 (Id) 错误

ios - 为 iOS 3.1.3 设备编译的 Xcode 4.2

ios - 如何在没有类名的情况下推送下一类而不是在 iOS 中使用 NSString?

ios - 在 NavigationBar 上带有隐藏 TableView 的 UISearchBar

ios - 从 Swift 函数中的异步调用返回数据

ios - Xamarin UITableView 不会滚动

ios - UITableview cellForRowAtIndexPath 方法没有执行?