UISearchBar显示错误:文本太远,与放大镜图标重叠 [英] UISearchBar display bug: text too far left, overlapping the magnifying glass icon

查看:90
本文介绍了UISearchBar显示错误:文本太远,与放大镜图标重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了我的 UISearchBar 中最奇怪的显示错误。

这是我的代码在另一个应用程序中完美运行。 (我在代码中创建搜索栏,因为xib文件不可行。)

I have encountered the weirdest display bug with my UISearchBar.
Here is my code that works perfectly in another app. (I create the search bar in code because a xib file is not feasible.)

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;

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

The search functionality etc. works fine. But strangely, the search bar looks like this, both on the device and the simulator:


  • 占位符字符串不应该在那里,而是在右边。

  • 当我输入搜索文本时,它也会向左开始,显示放大镜图标。

  • 此外,将 searchBar.placeholder searchBar.prompt 设置为 @ nil 不会删除此搜索占位符。 (但是,
    searchBar.placeholder = @FooBar;

    将显示FooBar,但仍在错误的立场。

  • 最后,我觉得奇怪的是字符串是搜索,虽然我已将语言设置为别的东西(比如德语)。

  • The placeholder string should not be there, but further to the right.
  • When I enter search text it also starts too far left, with the magnifying glass icon showing through.
  • Also, setting searchBar.placeholder or searchBar.prompt to @"" or nil does not remove this "Search" placeholder. (However, a
    searchBar.placeholder = @"FooBar";
    will display "FooBar", but still in the wrong position.
  • Finally, I find it strange that the string is "Search" although I have set the language to something else (say, German).

任何帮助?最紧迫的问题是让文本在右边。

Any help? The most pressing problem is to get the text to over the right.

推荐答案

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

That behavior can be caused by overriding either (CGRect)editingRectForBounds:(CGRect)bounds or - (CGRect)textRectForBounds:(CGRect)bounds. Have you altered either of those in a UITextField category elsewhere in your app? The purpose of doing that may have been to add padding to a text field (which can instead be accomplished with an invisible leftView). In this case, your custom "padding" is being applied to the UISearchBar text field.

UITextField 上的此类别将也会导致您的应用在初始化使用 UITextField 的框架类时崩溃,例如 MFMailComposeViewController ,所以这是一个坏主意有几个原因。

Such a category on UITextField will also cause your app to crash while initializing framework classes that use UITextField, such as MFMailComposeViewController, so it is a bad idea for several reasons.

这篇关于UISearchBar显示错误:文本太远,与放大镜图标重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆