Andrew Lauer Barinov
Andrew Lauer Barinov

Reputation: 5754

Shifting text in a UITextField when using leftView

Currently I am setting a leftView with an image and I am finding that the image is too close to the text input view (which in this case is the standard UITextField text input view). Is there a way to shift the frame of the text input view slightly so as to increase the distance between it and the leftView?

Upvotes: 3

Views: 3089

Answers (1)

Beltalowda
Beltalowda

Reputation: 4678

You should be able to modify the frame property for the UIImageView you're assigning to the UITextField's leftView.

For example you might be able to do something like this:

UIImageView *someImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"someImage"]];

// add 5 points to each side of the frame while keeping the same center point
someImageView.frame = CGRectInset(someImageView.frame, -5, 0);

textField.leftViewMode = UITextFieldViewModeAlways;
textField.leftView = someImageView;

Note that you may also need to modify the UIImageView's contentMode property to get the desired effect (e.g. you can use UIViewContentModeLeft to left-align the image or UIViewContentModeCenter to center-align it).

Upvotes: 3

Related Questions