我似乎无法覆盖 UITextField leftViewRectForBounds 方法

发布于 2024-10-23 23:31:42 字数 914 浏览 5 评论 0原文

大局观:这就是我正在努力实现的目标。向某人发送短信。 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:
enter image description here

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):
enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浴红衣 2024-10-30 23:31:42

你可以让你的类像下面这样

@interface MyTextField : UITextField
{
   //Declare your variable  here    
}
@end

@implementation MyTextFiled

// override leftViewRectForBounds method:
- (CGRect)leftViewRectForBounds:(CGRect)bounds{
    CGRect leftBounds = CGRectMake(bounds.origin.x + 30, 0, 45, 44);
    return leftBounds;
}

使用 MyTextField 而不是 UITextField。

you could have your class something like below

@interface MyTextField : UITextField
{
   //Declare your variable  here    
}
@end

@implementation MyTextFiled

// override leftViewRectForBounds method:
- (CGRect)leftViewRectForBounds:(CGRect)bounds{
    CGRect leftBounds = CGRectMake(bounds.origin.x + 30, 0, 45, 44);
    return leftBounds;
}

Use MyTextField instead of UITextField.

雨落星ぅ辰 2024-10-30 23:31:42

如果您没有将文本字段作为自定义文本字段,则不会调用该方法。要覆盖此方法,您必须采用继承 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文