Uitextfield Leftview/Rightview Padding on iOS7

UITextfield leftView/rightView padding on iOS7

Was just working on this 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 over from the right by 10 instead of having the image squeezed up against the edge in iOS 7.

Additionally, this was in a subclass of UITextField, which can be created by:

  1. Create a new file that's a subclass of UITextField instead of the default NSObject
  2. 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;
    }
  3. You may have to import #import <QuartzCore/QuartzCore.h>

  4. Add the rightViewRectForBounds method above

  5. In Interface Builder, click on the TextField you would like to subclass and change the class attribute to the name of this new subclass

How to get left padding on UITextField leftView image?

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;

How can I change UITextField leftview frame

To change the position of the leftView you need to create your own subclass of UITextField and override the leftViewRectForBounds: method to return a different frame.

How to toggle show and hide LeftView/RightView of UITextField?

In your hideIcon method, try self.inputTextField.leftView = nil and don't forget to call your hideIcon method.

Set padding for UITextField with UITextBorderStyleNone

I found a neat little hack to set the left padding for this exact situation.

Basically, you set the leftView property of the UITextField to be an empty view of the size of the padding you want:

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

Worked like a charm for me!

In Swift 3/ Swift 4, it can be done by doing that

let paddingView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: 5, height: 20))
textField.leftView = paddingView
textField.leftViewMode = .always

Position of rightView UITextField

The right overlay view is placed in the rectangle returned by the rightViewRectForBounds: method of the receiver.

So I suggest you subclass UITextField and override this method, something like this:

@interface CustomTextField: UITextField

@end

@implementation CustomTextField

// override rightViewRectForBounds method:
- (CGRect)rightViewRectForBounds:(CGRect)bounds{
CGRect rightBounds = CGRectMake(bounds.origin.x + 10, 0, 30, 44);
return rightBounds ;
}

UITextField leftView and rightView overlapping issue - iOS13

Swift 5 - iOS13

Prior to iOS 13, UITextField assumed that the frames of its leftView
and rightView were correctly set when assigned and would never change.

Starting in iOS 13, the implementation of leftViewRect(forBounds:) and rightViewRect(forBounds:) now ask the view for its systemLayoutSizeFitting(_:).

To achieve the previous behavior when linking against and running on iOS 13, add explicit sizing constraints on the view, wrap it in a plain UIView, or subclass the view and implement systemLayoutSizeFitting(_:)


Method override in textField class

override func rightViewRect(forBounds bounds: CGRect) -> CGRect {
return CGRect(x: bounds.width - 50, y: 0, width: 30 , height: bounds.height)
}

Reference link -
https://howtotechglitz.com/apple-has-just-released-ios-13-developer-beta-5-for-iphone-ios-iphone-gadget-hacks/

Hope it's helps to you.



Related Topics



Leave a reply



Submit