OGeek|极客世界-中国程序员成长平台

标题: ios - UISearchBar 'prompt' 显示不正确 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 19:29
标题: ios - UISearchBar 'prompt' 显示不正确

我已经像这样设置了一个 UISearchBarController:

self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchBar.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.prompt = @"Searching Transactions Since Nov 11 2017";
self.searchController.searchBar.placeholder = @"Search";

这是设置.prompt的输出:

enter image description here

这就是 View 呈现和滚动时的外观。 prompt 应该显示在搜索栏上方,如 the HIG 所示.

有谁知道它为什么会这样做?它是一个错误吗?还是 prompt 只是为了在设置单独的搜索 Controller 时正确显示?



Best Answer-推荐答案


不确定是否是 UISearchBar 的 bug,但可以添加偏移量:

self.searchController.searchBar.searchFieldBackgroundPositionAdjustment = UIOffsetMake(0, 20);

如果你有取消按钮:

UIBarButtonItem *cancelButton = [UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil];
[cancelButton setTitlePositionAdjustment:UIOffsetMake(0, 20) forBarMetrics:UIBarMetricsDefault];

关于ios - UISearchBar 'prompt' 显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47910089/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4