i will like to know how do i need to change a UISearchBar from the default round curve to a rectangle.
I also recently achieved this in a project by traversing the subviews of the UISearchBar object. However, the text field was not an immediate child as the accepted answer suggested, but a subview of another subview of it. The inner view hierarchy was probably changed at some point. (SDK 8.4)
+ (void)setSearchBar:(UISearchBar *)searchBar cornerRadius:(CGFloat)radius {
//set searchbar corner radius
for(UIView *possibleSearchBarTextFieldSuperview in [searchBar subviews]) {
if([[possibleSearchBarTextFieldSuperview subviews] count] > 0) {
for(UIView *possibleInnerSearchBarTextField in [possibleSearchBarTextFieldSuperview subviews]) {
if([possibleInnerSearchBarTextField conformsToProtocol:@protocol(UITextInputTraits)]) {
possibleInnerSearchBarTextField.layer.cornerRadius = radius;
possibleInnerSearchBarTextField.layer.masksToBounds = YES;
return;
}
}
}
}
}
This method does not use any undocumented methods so it's probably App Store safe, although it is susceptible to stop working with future changes to the SDK.