how to change UISearchBar from round to rectangle?

后端 未结 12 1552
没有蜡笔的小新
没有蜡笔的小新 2021-02-09 01:54

i will like to know how do i need to change a UISearchBar from the default round curve to a rectangle.

\"enter

12条回答
  •  遥遥无期
    2021-02-09 02:21

    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.

提交回复
热议问题