UISearchBar显示错误:文本太远,与放大镜图标重叠

Mun*_*ndi 5 objective-c uisearchbar ios

我遇到了最怪异的显示错误UISearchBar.
这是我的代码在另一个应用程序中完美运行.(我在代码中创建搜索栏,因为xib文件不可行.)

searchBar = [[UISearchBar alloc] initWithFrame:
    CGRectMake(0, 0, self.tableView.frame.size.width, 44)];
searchBar.delegate = self;
searchController = [[UISearchDisplayController alloc] 
                   initWithSearchBar:searchBar contentsController:self];
searchController.delegate = self;
searchController.searchResultsDataSource = self;
searchController.searchResultsDelegate = self;
self.tableView.tableHeaderView = searchBar;
Run Code Online (Sandbox Code Playgroud)

搜索功能等工作正常.但奇怪的是,搜索栏在设备和模拟器上都是这样的:

带有显示错误的UISearchBar

  • 占位符字符串不应该在那里,而是在右边.
  • 当我输入搜索文本时,它也会向左开始,显示放大镜图标.
  • 另外,设置searchBar.placeholdersearchBar.prompt@""nil不移除此"搜索"占位符.(但是,a
    searchBar.placeholder = @"FooBar";
    会显示"FooBar",但仍然处于错误的位置.
  • 最后,我发现奇怪的是字符串是"搜索",尽管我已经将语言设置为其他东西(例如,德语).

有帮助吗?最紧迫的问题是将文本置于右侧.

ipa*_*son 19

这种行为可能是由于覆盖了(CGRect)editingRectForBounds:(CGRect)bounds或者- (CGRect)textRectForBounds:(CGRect)bounds.您是否更改了UITextField应用中其他类别中的任何一个?这样做的目的可能是为文本字段添加填充(可以用不可见的方式完成leftView).在这种情况下,您的自定义"填充"将应用于UISearchBar文本字段.

这样的类别UITextField也将导致您的应用程序在初始化使用的框架类时崩溃UITextField,例如MFMailComposeViewController,因此出于几个原因这是一个坏主意.