user4226071
user4226071

Reputation:

UITextField background property issue in iOS6

I would like to create UITextField which looks like a Label in normal view and as Text field with custom background in edit mode. I added a "No Border Style" UITextField in XIB and set the outlet with name nameTextField. I have set a background for UITextField in edit mode and nil background in view mode on button event, but the image is not appearing after the first disappear in iOS 6.x, everything else works as expected. I have tested in iOS 7.x and it works as expected. The code looks like this,

- (IBAction)editButtonTapped:(id)sender
{
    editMode = !editMode;
    if (editMode)
    {
        nameTextField.background = [UIImage imageNamed:@"textbg.png"];
        nameTextField.enabled = YES;
    }
    else
    {
        nameTextField.background = nil;
        nameTextField.enabled = NO;
    }
}

Test case :

  1. Tap Edit Button, to enable editing and show background -> Background appears and field is enabled as expected
  2. Tap Edit Button again, to disable editing and hide background -> Background disappears as expected
  3. Tap Edit Button again, to Enable edit and show background -> Field is enabled for editing but without background.

Upvotes: 2

Views: 157

Answers (2)

user4226071
user4226071

Reputation:

Thx for your comments.

I think this one is an iOS 6.x bug; if the background property of UITextField set to nil on or after viewDidAppear, later changes to the background property will not be drawn.

The workaround for this issue is to use a blank transparent image instead of nil. The image can be used in either of the two ways,

1 ) Set the background (textbg.png and blank_image.png) through code; then the code will look like,

- (IBAction)editButtonTapped:(id)sender
{
    editMode = !editMode;
    if (editMode)
    {
        nameTextField.background = [UIImage imageNamed:@"textbg.png"];
        nameTextField.enabled = YES;
    }
    else
    {
        nameTextField.background = [UIImage imageNamed:@"blank_image.png"];
        nameTextField.enabled = NO;
    }
}

2 ) Set the background (textbg.png) and disabled background (blank_image.png) in XIB; then the code will look like,

- (IBAction)editButtonTapped:(id)sender
{
    editMode = !editMode;
    if (editMode)
    {
        nameTextField.enabled = YES;
    }
    else
    {
        nameTextField.enabled = NO;
    }
}

Upvotes: 1

Suresh Thoutam
Suresh Thoutam

Reputation: 258

Try to do this

In the Interface Builder, just below the place where you set the background image, there should be an option to select a border style, by default it is selected to RoundedRect, select another style and you can immediately see the background image.

Upvotes: 0

Related Questions