How can I move the clear button in a UITextField?

Multi tool use
Multi tool use
The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How can I move the clear button in a UITextField?



For some reason, when I add a UITextfield as a subview of the contentview of a tablecell, the clearbutton does not align with the text typed in the field, and appears a bit underneath it. Is there any way I can move the text of the clearbutton to stop this from happening? Thanks for any help,




6 Answers
6



I haven't seen this, and a screen shot would be helpful. But, the quick answer is that you can inspect the subviews array of the UITextField, find the subview that contains the clear button, and adjust its frame.origin.



Edit: It seems I've been downvoted for this answer (written in 2010). This is of not an "officially" approved method because you're manipulating private objects, but it's not detectable by Apple. The main risk is that the view hierarchy might be changed at some point.





Thanks for the tip, I'm on the road right now but once I get to a stop, I'll try this out and update you.
– Jake
May 29 '10 at 15:17





Yeah im just going to move the subview.
– Jake
May 31 '10 at 4:50





The code of UITextField.m is not accessible. UITextField should be subclassed
– Luda
Feb 14 '13 at 8:22





Yes, You can't access the .m of UITextField. And further more, you should not access it. You can change exposed properties or subclass. But the provided solution is wrong.
– Luda
Feb 20 '13 at 7:37





If "apple doesn't want you to do it", then probably apple plans to change it in future. If you are okay with fixing stuff here and there for random sdkiOS releases, then you can go this way. Otherwise, you should follow their guidelines
– igrek
Nov 14 '16 at 16:57





As stated by @Luda the correct way is to subclass UITextField and override - (CGRect)clearButtonRectForBounds:(CGRect)bounds. However the bounds passed in to the method are those of the view itself and not the button. Therefore you should call super to get the OS provided size (to avoid distortion of the image) and then adjust the origin to suit your needs.


- (CGRect)clearButtonRectForBounds:(CGRect)bounds


super



e.g.


- (CGRect)clearButtonRectForBounds:(CGRect)bounds {
CGRect originalRect = [super clearButtonRectForBounds:bounds];
return CGRectOffset(originalRect, -10, 0); //shift the button 10 points to the left
}



Apple docs state:



Discussion You should not call this method directly. If you want to
place the clear button in a different location, you can override this
method and return the new rectangle. Your method should call the super
implementation and modify the returned rectangle’s origin only.
Changing the size of the clear button may cause unnecessary distortion
of the button image.



I had subclassed the UITextField and override the function clearButtonRectForBounds:.


UITextField


clearButtonRectForBounds:



.h


#import <UIKit/UIKit.h>

@interface TVUITextFieldWithClearButton : UITextField

@end



.m


#import "TVUITextFieldWithClearButton.h"

@implementation TVUITextFieldWithClearButton

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}

- (void)awakeFromNib
{
self.clearButtonMode = UITextFieldViewModeWhileEditing;
}


- (CGRect)clearButtonRectForBounds:(CGRect)bounds
{
return CGRectMake(bounds.size.width/2-20 , bounds.origin.y-3, bounds.size.width, bounds.size.height);
}

@end





It appears that you've downvoted the other answers. Not cool.
– Simon Woodside
Feb 20 '13 at 4:21





I down-voted one of the answers. I did it because the answer, in my opinion, is wrong. I have commented why I think so, and I will be glad to continue talking to you about that (on the relevant thread). Most importent is to understand how are things done right and not how cool are you.
– Luda
Feb 20 '13 at 7:41



Subclass UITextField and override this method:


- (CGRect)clearButtonRectForBounds:(CGRect)bounds
{
return CGRectMake(bounds.origin.x - 10, bounds.origin.y, bounds.size.width, bounds.size.height);
}



return the CGRect that matches your needs.





This wont put the button in the right edge. This will: return CGRectMake(bounds.size.width/2-20 , bounds.origin.y-3, bounds.size.width, bounds.size.height);
– Luda
Feb 14 '13 at 8:24



Swift 4 version would be


import UIKit

class LoginTextField: UITextField {

override func clearButtonRect(forBounds bounds: CGRect) -> CGRect {
return CGRect(x: xPos, y:yPos, width: yourWidth, height: yourHeight)
}

}



The answer from Van Du Tran in Swift 4:


class CustomTextField: UITextField {


override func clearButtonRect(forBounds bounds: CGRect) -> CGRect {
let originalRect = super.clearButtonRect(forBounds: bounds)

return originalRect.offsetBy(dx: -8, dy: 0)
}



}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

fwYfaQ2JfCJIejvvEYokqzVGf54P5vTSi3,lxAyW 1aulroSZpULE,ZqKOZZ1QB2UD0ybukSauq0GGO5zchFQuq
9elSp1bV1Go,10,IZgR0e53yQXdbt9wkeb8CO3gsiX0ulX cLM,I,GGX8NliMJ C05E61V,4ij1EcKF9 YmSRD,HMJxv,Jn6CDefa7emJb3

Popular posts from this blog

Makefile test if variable is not empty

Visual Studio Code: How to configure includePath for better IntelliSense results

Will Oldham