我想知道如何将UISearchBar从默认的圆形曲线更改为矩形。

最佳答案

    for (UIView *searchBarSubview in [mySearchBar subviews]) {
        if ([searchBarSubview conformsToProtocol:@protocol(UITextInputTraits)]) {
            @try {

                [(UITextField *)searchBarSubview setBorderStyle:UITextBorderStyleRoundedRect];
            }
            @catch (NSException * e) {
                // ignore exception
            }
        }
    }

Swift 4:
mySearchBar.subviews().forEach { searchBarSubview in
  if searchBarSubview is UITextInputTraits {
    do {
        (searchBarSubview as? UITextField)?.borderStyle = .roundedRect
    } catch {
        // ignore exception
    }
  }
}

10-05 23:13