Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get left padding on UITextField leftView image?

I am setting up a UIImageView as a leftView on a UITextField like so:

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;

which gets me the following:

UITextField image

As you can see the left size of the image goes right up to the left edge of the button even though I tried to set an inset. How can I move this envelope in so that it's got padding on all sides?


Update: I tried the proposed answer of changing the UIImageView frame like so, but the envelope is still lined up 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)];
like image 936
helloB Avatar asked Sep 11 '15 13:09

helloB


3 Answers

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

like image 155
Fitsyu Avatar answered Sep 26 '22 18:09

Fitsyu


For Swift 4.2 +

You can use this extension:

extension UITextField {
    func leftImage(_ image: UIImage?, imageWidth: CGFloat, padding: CGFloat) {
        let imageView = UIImageView(image: image)
        imageView.frame = CGRect(x: padding, y: 0, width: imageWidth, height: frame.height)
        imageView.contentMode = .center
        
        let containerView = UIView(frame: CGRect(x: 0, y: 0, width: imageWidth + 2 * padding, height: frame.height))
        containerView.addSubview(imageView)
        leftView = containerView
        leftViewMode = .always
    }
}
like image 30
RunesReader Avatar answered Sep 23 '22 18:09

RunesReader


enter image description hereyou can simply try this:

    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;
like image 39
Teja Nandamuri Avatar answered Sep 26 '22 18:09

Teja Nandamuri