Reputation:
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 :
Upvotes: 2
Views: 157
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
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