我似乎无法覆盖 UITextField leftViewRectForBounds 方法
大局观:这就是我正在努力实现的目标。向某人发送短信。 UI/UX 应该与此类似:
因此,目前我正在使用带有 3 个单元格的 UITableView。前两个(“收件人:”和“发件人:”)高 44 像素。我将 UITextField 放入 UITableViewCell 的“contentView”内。一切都很好。我可以让用户编辑单元格的内容。
现在,我使用 leftView 属性来显示带有文本“To:”的 UILabel。它有效,但“收件人”标签始终位于 UITextField 的最左侧(如下图所示):
因此,根据 SDK 文档。左视图模式由以下方法决定: leftViewRectForBounds:
根据讨论:
您不应该调用此方法 直接地。如果您想放置 左覆盖视图在不同的 位置,您可以重写此方法 并返回新的矩形。
因此,我尝试覆盖但没有取得太大成功。如果有人有任何代码片段,我将不胜感激。在下图中,我将“From”设置为“From”,这有点“破解”它,但我想正确地做到这一点。这就是我尝试过的:
// override leftViewRectForBounds method:
- (CGRect)leftViewRectForBounds:(CGRect)bounds{
CGRect leftBounds = CGRectMake(bounds.origin.x + 30, 0, 45, 44);
return leftBounds;
}
以及它的小变体,但它似乎不起作用,因此感谢任何帮助。谢谢!!!
Big picture: This is what I am trying to accomplish. Send a text message to someone. The UI/UX should look something similar to this:
So, currently, I am using UITableView with 3 cells. The first two (for "To:" and "From:") are 44px high. I put a UITextField inside the "contentView" of a UITableViewCell. Everything is good. I can get the user to edit the content of the cell.
Now, I am using the leftView property to display a UILabel with text "To:". It works but the "To" label is always on the very left of the UITextField (shown in img below):
So, according to the SDK documentation. The left view mode is dictated by this method:
leftViewRectForBounds:
According to the discussion:
You should not call this method
directly. If you want to place the
left overlay view in a different
location, you can override this method
and return the new rectangle.
So, I tried overriding without much success. If anyone have any code snippets, I would greatly appreciate it. In the bottom picture, I set the "From" to be " From" which is "hacking" it a little bit but I would like to do it correctly. This is what I have tried:
// override leftViewRectForBounds method:
- (CGRect)leftViewRectForBounds:(CGRect)bounds{
CGRect leftBounds = CGRectMake(bounds.origin.x + 30, 0, 45, 44);
return leftBounds;
}
and small variants of it and it doesn't seem to work so any help is appreciated. Thanks!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以让你的类像下面这样
使用 MyTextField 而不是 UITextField。
you could have your class something like below
Use MyTextField instead of UITextField.
如果您没有将文本字段作为自定义文本字段,则不会调用该方法。要覆盖此方法,您必须采用继承 UITextField 的自定义文本字段类。
If you have not taken the textField as customtextfield the method will not be called.To override this method you have to take custom textField class which inherits UITextField.