UITextfield leftView / rightView padding on iOS7

The leftView and rightView UITextField views on iOS7 are really close to the border of the text field.

How to add some (horizontal) additions to these elements?

I tried modifying the frame but did not work

uint padding = 10;//padding for iOS7 UIImageView * iconImageView = [[UIImageView alloc] initWithImage:iconImage]; iconImageView.frame = CGRectMake(0 + padding, 0, 16, 16); textField.leftView = iconImageView; 

Please note that I am not interested in adding an addendum to text text, for example Install an addendum for UITextField with UITextBorderStyleNone

+86
ios objective-c uitextfield
Oct. 14 '13 at 23:39
source share
23 answers

Just worked on it myself and used this solution:

 - (CGRect) rightViewRectForBounds:(CGRect)bounds { CGRect textRect = [super rightViewRectForBounds:bounds]; textRect.origin.x -= 10; return textRect; } 

This will move the image to the right by 10 instead of compressing the image against the edge in iOS 7.

In addition, this was in a subclass of UITextField that could be created:

  • Create a new subclass file UITextField instead of the standard NSObject
  • Add a new method named - (id)initWithCoder:(NSCoder*)coder to set the image

     - (id)initWithCoder:(NSCoder*)coder { self = [super initWithCoder:coder]; if (self) { self.clipsToBounds = YES; [self setRightViewMode:UITextFieldViewModeUnlessEditing]; self.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"textfield_edit_icon.png"]]; } return self; } 
  • You may need to import #import <QuartzCore/QuartzCore.h>

  • Add the rightViewRectForBounds method above

  • In the Builder interface, click on the TextField that you want to subclass, and change the class attribute to the name of this new subclass

+82
Oct. 15 '13 at 4:10
source share

A much simpler solution that uses contentMode :

  arrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"down_arrow"]]; arrow.frame = CGRectMake(0.0, 0.0, arrow.image.size.width+10.0, arrow.image.size.height); arrow.contentMode = UIViewContentModeCenter; textField.rightView = arrow; textField.rightViewMode = UITextFieldViewModeAlways; 

In Swift 2,

  let arrow = UIImageView(image: UIImage(named: "down_arrow")) arrow.frame = CGRectMake(0.0, 0.0, arrow.image.size.width+10.0, arrow.image.size.height); arrow.contentMode = UIViewContentMode.Center self.textField.rightView = arrow self.textField.rightViewMode = UITextFieldViewMode.Always 

In Swift 3

  let arrow = UIImageView(image: UIImage(named: "arrowDrop")) if let size = arrow.image?.size { arrow.frame = CGRect(x: 0.0, y: 0.0, width: size.width + 10.0, height: size.height) } arrow.contentMode = UIViewContentMode.center self.textField.rightView = arrow self.textField.rightViewMode = UITextFieldViewMode.always 
+163
Jun 24 '14 at 13:53 on
source share

The easiest way is to add a UIView to leftView / righView and add an ImageView to a UIView, configure the ImageView source inside the UIView anywhere, it worked for me like a charm. He needs only a few lines of code

 UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 26, 26)]; imgView.image = [UIImage imageNamed:@"img.png"]; UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 32, 32)]; [paddingView addSubview:imgView]; [txtField setLeftViewMode:UITextFieldViewModeAlways]; [txtField setLeftView:paddingView]; 
+45
Jun 27 '14 at 11:48
source share

This works fine for Swift:

 let imageView = UIImageView(image: UIImage(named: "image.png")) imageView.contentMode = UIViewContentMode.Center imageView.frame = CGRectMake(0.0, 0.0, imageView.image!.size.width + 20.0, imageView.image!.size.height) textField.rightViewMode = UITextFieldViewMode.Always textField.rightView = imageView 
+13
Jul 31 '15 at 17:08
source share

It works for me

 UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 20)]; self.passwordTF.leftView = paddingView; self.passwordTF.leftViewMode = UITextFieldViewModeAlways; 

May this help you.

+7
Aug 01 '15 at 4:57
source share

I like this solution because it solves the problem with a single line of code

 myTextField.layer.sublayerTransform = CATransform3DMakeTranslation(10.0f, 0.0f, 0.0f); 

Note: .. or 2 if you consider including QuartzCore in a string :)

+5
Dec 24 '14 at 11:01
source share

The best way to do this is to simply create a class using a subclass of UITextField and in the .m file

  #import "CustomTextField.h" #import <QuartzCore/QuartzCore.h> @implementation CustomTextField - (id)initWithCoder:(NSCoder*)coder { self = [super initWithCoder:coder]; if (self) { //self.clipsToBounds = YES; //[self setRightViewMode:UITextFieldViewModeUnlessEditing]; self.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,15,46)]; self.leftViewMode=UITextFieldViewModeAlways; } return self; } 

after doing this, go to your storyboard or xib and click on the identity inspector and replace UITextfield with your own "CustomTextField" in the class option.

Note. If you just pass the registration with the automatic layout for the text field, your application will not start and display only a blank screen.

+3
Mar 19 '15 at 8:26
source share

I found this somewhere ...

 UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)]; paddingView.backgroundColor = [UIColor clearColor]; itemDescription.leftView = paddingView; itemDescription.leftViewMode = UITextFieldViewModeAlways; [self addSubview:itemDescription]; 
+2
Oct. 14 '13 at 23:45
source share

Create a custom UITextField class and use this class instead of UITextField. Override - (CGRect) textRectForBounds: (CGRect) restricts the setting of the required rectangle

Example

 - (CGRect)textRectForBounds:(CGRect)bounds{ CGRect textRect = [super textRectForBounds:bounds]; textRect.origin.x += 10; textRect.size.width -= 10; return textRect; } 
+1
Oct. 15 '13 at 0:23
source share

I had this problem myself, and by far the easiest solution is to change your image to just add an addition to each side of the image!

I just modified my png image to add a transparent add-on of 10 pixels and it works well, without coding at all!

+1
Aug 13 '15 at 9:11
source share

Here is one solution:

  UIView *paddingTxtfieldView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 42)]; // what ever you want txtfield.leftView = paddingTxtfieldView; txtfield.leftViewMode = UITextFieldViewModeAlways; 
0
Oct. 15 '13 at 5:32
source share

One trick: add a UIView containing a UIImageView to the UITextField, as a rightView. This UIView should be larger, now put the UIImageView to the left of it. Thus, there will be free space on the right.

 // Add a UIImageView to UIView and now this UIView to UITextField - txtFieldDate UIView *viewRightIntxtFieldDate = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 30)]; // (Height of UITextField is 30px so height of viewRightIntxtFieldDate = 30px) UIImageView *imgViewCalendar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 10, 10, 10)]; [imgViewCalendar setImage:[UIImage imageNamed:@"calendar_icon.png"]]; [viewRightIntxtFieldDate addSubview:imgViewCalendar]; txtFieldDate.rightViewMode = UITextFieldViewModeAlways; txtFieldDate.rightView = viewRightIntxtFieldDate; 
0
Jan 14 '14 at 11:38 on
source share

I created my own method in my ViewController class, as shown below:

 - (void) modifyTextField:(UITextField *)textField { // Prepare the imageView with the required image uint padding = 10;//padding for iOS7 UIImageView * iconImageView = [[UIImageView alloc] initWithImage:iconImage]; iconImageView.frame = CGRectMake(0 + padding, 0, 16, 16); // Set the imageView to the left of the given text field. textField.leftView = iconImageView; textField.leftViewMode = UITextFieldViewModeAlways; } 

Now I can call this method inside ( viewDidLoad method) and send any of my TextFields this method and add indentation both on the right and on the left and give the text and background colors by writing only one line of code, as follows:

 [self modifyTextField:self.firstNameTxtFld]; 

This works great on iOS 7! Hope this still works on iOS 8 and 9!

I know that adding too many views can make this a heavier object to load. But when we are concerned about the difficulty of other solutions, I find myself more biased towards this method and more flexible using this method .;)

Hope this answer may be helpful or helpful in figuring out another solution for someone else.

Hurrah!

0
Mar 18 '14 at 5:38
source share

The following is an example of adding a horizontal spacer to the left view, which is an icon - you can use a similar approach to add a complement to any UIView that you would like to use as the field on the left of the field.

Inside the UITextField subclass:

 static CGFloat const kLeftViewHorizontalPadding = 10.0f; @implementation TextFieldWithLeftIcon { UIImage *_image; UIImageView *_imageView; } - (instancetype)initWithFrame:(CGRect)frame image:(UIImage *)image { self = [super initWithFrame:frame]; if (self) { if (image) { _image = image; _imageView = [[UIImageView alloc] initWithImage:image]; _imageView.contentMode = UIViewContentModeCenter; self.leftViewMode = UITextFieldViewModeAlways; self.leftView = _imageView; } } return self; } #pragma mark - Layout - (CGRect)leftViewRectForBounds:(CGRect)bounds { CGFloat widthWithPadding = _image.size.width + kLeftViewHorizontalPadding * 2.0f; return CGRectMake(0, 0, widthWithPadding, CGRectGetHeight(bounds)); } 

Although we are a subclass of UITextField here, I believe this is the cleanest approach.

0
Dec 23 '14 at 0:03
source share
 - (CGRect)rightViewRectForBounds:(CGRect)bounds { return CGRectMake(bounds.size.width - 40, 0, 40, bounds.size.height); } 
0
Apr 21 '15 at 9:59
source share

I thank you guys for your answers, to my surprise, not one of them placed a suitable image in my text box, but at the same time provided the necessary indentation. then I thought about using AspectFill mode and miracles happened. for future seekers, here is what I used:

 UIImageView *emailRightView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 35, 35)]; emailRightView.contentMode = UIViewContentModeScaleAspectFill; emailRightView.image = [UIImage imageNamed:@"icon_email.png"]; emailTextfield.rightViewMode = UITextFieldViewModeAlways; emailTextfield.rightView = emailRightView; 

The 35 in the frame of my image represents the height of my email field, feel free to tailor it to your needs.

0
May 14 '15 at 19:32
source share

If you use UIImageView as a leftView, you should use this code:

Caution: do not use inside viewWillAppear or viewDidAppear

 -(UIView*)paddingViewWithImage:(UIImageView*)imageView andPadding:(float)padding { float height = CGRectGetHeight(imageView.frame); float width = CGRectGetWidth(imageView.frame) + padding; UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)]; [paddingView addSubview:imageView]; return paddingView; } 
0
Sep 20 '15 at 19:30
source share

Simple code in Swift:

 let paddleView:UIView = UIView(frame: CGRect(x:0, y:0, width:8, height:20)); uiTextField.leftView = paddleView; uiTextField.leftViewMode = UITextFieldViewMode.Always; 
0
Dec 23 '15 at 8:51
source share

The easiest way is to simply change the text box as RoundRect instead of Custom and see the magic. :)

0
Mar 17 '16 at 12:25
source share

for Swift2, I use

 ... self.mSearchTextField.leftViewMode = UITextFieldViewMode.Always let searchImg = UIImageView(image: UIImage(named: "search.png")) let size = self.mSearchTextField.frame.height searchImg.frame = CGRectMake(0, 0, size,size) searchImg.contentMode = UIViewContentMode.ScaleAspectFit self.mSearchTextField.leftView = searchImg ... 
0
May 23 '16 at 15:07
source share

Instead of manipulating the imageView or image, we can override the method provided by apple for the rightView.

 class CustomTextField : UITextField { override func rightViewRect(forBounds bounds: CGRect) -> CGRect { let offset = 5 let width = 20 let height = width let x = Int(bounds.width) - width - offset let y = offset let rightViewBounds = CGRect(x: x, y: y, width: width, height: height) return rightViewBounds }} 

and in the same way we can redefine below func for left view.

 override func leftViewRect(forBounds bounds: CGRect) -> CGRect { /*return as per requirement*/ } 
0
Mar 07 '19 at 10:40
source share
 ... textField.rightView = UIImageView(image: ...) textField.rightView?.contentMode = .top textField.rightView?.bounds.size.height += 10 textField.rightViewMode = .always ... 
0
Apr 25 '19 at 22:30
source share

Simple approach:

 textField.rightViewMode = .always let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 25, height: 15)) textField.contentMode = .scaleAspectFit imageView = UIImage(named: "imageName") textField.rightView = imageView 

Note. The height should be less than the width to ensure horizontal filling.

0
Jun 19 '19 at 3:48
source share



All Articles