2011-03-17 94 views
1

大图:这是我想要完成的。发送短信给某人。 UI/UX应该看起来类似于这样的: enter image description here我似乎无法重写UITextField leftViewRectForBounds方法

所以,目前,我正在使用3个单元的UITableView。前两个(“To:”和“From:”)高44px。我在UITableViewCell的“contentView”中放置了一个UITextField。一切都很好。我可以让用户编辑单元格的内容。

现在,我使用leftView属性来显示带有文本“To:”的UILabel。它可以工作,但“To”标签总是在UITextField的左侧(如下图所示): enter image description here

所以根据SDK文档。左视图模式是通过这种方法决定: leftViewRectForBounds:

根据讨论:

你不应该直接调用此方法 。如果要将 左侧覆盖视图置于不同的 位置,则可以覆盖此方法 并返回新的矩形。

所以,我试图重写没有太大的成功。如果有人有任何代码片段,我将不胜感激。在底部的图片中,我将“发件人”设置为“发件人”,这是“黑客入侵”的一小部分,但我想正确地做。这是我已经尝试过:

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

和它的小变种,它似乎并不工作,所以任何帮助表示赞赏。谢谢!!!

回答

4

你可以有你的类像下面

@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。

+1

完美的作品。谢谢! – okysabeni 2011-03-24 05:43:28

0

如果您还没有将textField作为customtextfield,则该方法将不会被调用。要覆盖此方法,您必须采用继承了UITextField的自定义textField类。