Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enter a rightview UIImageView to a UITextField

I have a target action that when a button is pressed, I validate the UITextField:

// Get the text from the UITextField
NSString *nameStr = _name.text;

_name.rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error.png"]];
[self.view addSubview:_name];

if (nameStr.length == 0)
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid Name"
                                                    message:@"You must enter a name."
                                               delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}

My UITextField are added to the view in viewDidLoad like so:

[self.view addSubview:_name];

How do I make a rightView UIImageView appear?

like image 311
cdub Avatar asked Oct 08 '13 06:10

cdub


1 Answers

For Swift 3.To show image before placeholder text in UITextField

func setPaddingView(strImgname: String,txtField: UITextField){
        let imageView = UIImageView(image: UIImage(named: strImgname))
        imageView.frame = CGRect(x: 0, y: 5, width: imageView.image!.size.width , height: imageView.image!.size.height)
        let paddingView: UIView = UIView.init(frame: CGRect(x: 0, y: 0, width: 50, height: 30))
        paddingView.addSubview(imageView)
        txtField.leftViewMode = .always
        txtField.leftView = paddingView
    }

To call Function

self.setPaddingView(strImgname: "mail", txtField: txtEmail)

enter image description here

like image 166
Hardik Thakkar Avatar answered Sep 24 '22 05:09

Hardik Thakkar