Rejeesh Rajan
Rejeesh Rajan

Reputation: 1019

addsubview method not working in the case of UITextField in iOS 7

I have a cross (x) button which I added as a subview to my textfield. The purpose of this button is to erase all user typed text when the user clicks the cross button. This was working perfectly in iOS 5 and 6. However the same code [textfield addSubview: btn] when I run in iOS 7 doesn't seem to work. The cross button is not visible when I run the App. When I debugged the code, I observed the button is not added to the subviews array of the textfield. Please tell me if there is any other alternative method to add the cross button as a subview to the textfield. Thanks in advance.

Upvotes: 1

Views: 1698

Answers (2)

Shaik Riyaz
Shaik Riyaz

Reputation: 11462

you don't have to add extra subview to a textfield to clear user typed text , UITextfield already has a property to enable it .

try the following .....

    UITextField * pTextfield = [[UITextField alloc] initWithFrame:CGRectMake(10, 100,100,30)];

    [pTextfield setBorderStyle:UITextBorderStyleRoundedRect];

    [pTextfield setClearButtonMode:UITextFieldViewModeAlways];

    [self.view addSubview:pTextfield];

Upvotes: 0

Jasmin Mistry
Jasmin Mistry

Reputation: 1476

Use left view for textfield. see exp Assume self.login is uitextfield

UIView *uipadd_login = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 45)];
self.login.rightViewMode = UITextFieldViewModeAlways;
self.login.rightView = uipadd_login;

Upvotes: 2

Related Questions