Iphone – How to change the UITextField so it looks like the Search Text Field

cocoa-touchiphoneiphone-sdk-3.0uitextfield

I would like to change the standard iPhone UITextField so that it looks exactly like the search text field within the UISearchBar. You can see an example of this for the SMS text entry field within the iPhone Messages application.

I do not believe that the UITextField has a style property that will meet this requirement. Any ideas?

Best Answer

If you want, you can actually extract the background image from a uisearch bar and make it a stretchable image to use as the background of a UITextField. I did the same thing and it works like a charm for me. Here's the code i used to extract it:

UIImage *img = [[[self.searchBar subviews] objectAtIndex:1] background];
NSData *imgdata = UIImagePNGRepresentation(img);

NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectoryPath = [searchPaths objectAtIndex: 0];

[imgdata writeToFile:[documentsDirectoryPath stringByAppendingPathComponent:@"searchbarbg.png"] atomically:YES];

the second subview of the uisearchbar is the actual text field; this is subject to change in any future os releases though.

this will save the PNG to the app's documents directory, where you can grab it and drop it into your project and use it as the text field's background. the left and top cap widths for the stretchable image are 15 pixels.