How to get left registration on a UITextField leftView image?

I set UIImageView as leftView on a UITextField like this:

 UIImageView *envelopeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.height*.1, self.height*.1)]; envelopeView.image = [UIImage imageNamed:@"envelope.png"]; envelopeView.contentMode = UIViewContentModeScaleAspectFit; envelopeView.bounds = CGRectInset(envelopeView.frame, 15, 10); self.emailAddress.leftView = envelopeView; self.emailAddress.leftViewMode = UITextFieldViewModeAlways; 

who gets the following from me:

UITextField image

As you can see, the left image size goes straight to the left edge of the button, although I tried to set the insert. How can I move this envelope so that it gets indented from all sides?


Update: I tried the suggested answer about changing the UIImageView frame UIImageView this, but the envelope is still aligned on the left side at the border of the UITextField :

 CGFloat padding = 20; UIImageView *envelopeView = [[UIImageView alloc] initWithFrame:CGRectMake(3*padding, padding, self.height*.1-padding, self.height*.1-padding)]; 
+5
source share
3 answers

enter image description here you can just try the following:

  UIImageView *envelopeView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 0, 30, 30)]; envelopeView.image = [UIImage imageNamed:@"comment-128.png"]; envelopeView.contentMode = UIViewContentModeScaleAspectFit; UIView *test= [[UIView alloc]initWithFrame:CGRectMake(20, 0, 30, 30)]; [test addSubview:envelopeView]; [self.textField.leftView setFrame:envelopeView.frame]; self.textField.leftView =test; self.textField.leftViewMode = UITextFieldViewModeAlways; 
+2
source

For Swift 3 users

Here is what worked for me:

 extension UITextField { /// set icon of 20x20 with left padding of 8px func setLeftIcon(_ icon: UIImage) { let padding = 8 let size = 20 let outerView = UIView(frame: CGRect(x: 0, y: 0, width: size+padding, height: size) ) let iconView = UIImageView(frame: CGRect(x: padding, y: 0, width: size, height: size)) iconView.image = icon outerView.addSubview(iconView) leftView = outerView leftViewMode = .always } } 

Test:

 txOrigin.setLeftIcon(icon_location) 

result:

enter image description here

+5
source

You can use this. Change the frame to suit your needs.

 NSTextAttachment* placeholderImageTextAttachment = [[NSTextAttachment alloc] init]; placeholderImageTextAttachment.image = [UIImage imageNamed:@"Search"]; placeholderImageTextAttachment.bounds = CGRectMake(0, -2, 16, 16); NSMutableAttributedString* placeholderImageString = [[NSAttributedString attributedStringWithAttachment:placeholderImageTextAttachment] mutableCopy]; NSMutableAttributedString* placeholderString = [[NSMutableAttributedString alloc] initWithString:NSLocalizedString(@" Search", nil)]; [placeholderImageString appendAttributedString:placeholderString]; _txtFieldSearch.attributedPlaceholder = placeholderImageString; _txtFieldSearch.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
0
source

All Articles