I cannot seem to override UITextField leftViewRectForBounds method
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 tw开发者_如何转开发o (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!!!
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.
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.
精彩评论