How do I change the UITextField so it looks like the Search Text Field?

纵然是瞬间 提交于 2019-12-06 08:43:08

问题


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?


回答1:


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.




回答2:


What about UITextBorderStyleRoundedRect?

textField.borderStyle = UITextBorderStyleRoundedRect;

You can also just use a UISearchBar and handle the text manually yourself.




回答3:


It seems that this is not supported within iPhone OS 3.0. I just used a transparent UITextField with UIImageView as the background. I believe this is how the official iPhone SMS text entry works.



来源:https://stackoverflow.com/questions/1968118/how-do-i-change-the-uitextfield-so-it-looks-like-the-search-text-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!