how to change UISearchBar from round to rectangle?

后端 未结 12 1540
没有蜡笔的小新
没有蜡笔的小新 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:10

    Much better in my opinion:

    UITextField *txfSearchField = [_searchBar valueForKey:@"_searchField"];
    txfSearchField.borderStyle = UITextBorderStyleNone;
    
    0 讨论(0)
  • 2021-02-09 02:11
        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
        }
      }
    }
    
    0 讨论(0)
  • 2021-02-09 02:15

    Swift 3

        let textFieldInsideUISearchBar = searchBar.value(forKey: "searchField") as? UITextField
        textFieldInsideUISearchBar?.borderStyle = .none
        textFieldInsideUISearchBar?.backgroundColor = UIColor.white
    
    0 讨论(0)
  • 2021-02-09 02:17

    Simple add background image, left view and right view for your TextField.

    Example below

    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 10., nameField.bounds.size.height)];
    nameField.leftView = leftView;
    nameField.leftViewMode = UITextFieldViewModeAlways;
    leftView release];
    
    0 讨论(0)
  • 2021-02-09 02:17

    In the target image I don't see the default shadow, so I'll mention that setting the background property to nil will remove it. I needed to do this in my project and removing the image and manipulating the border works well. I had to cast the control to get to the background property.

    UITextField * x = (UITextField*)searchBarSubview;
    x.background = nil;
    

    @Keller thanks for the tip for finding the control.

    0 讨论(0)
  • 2021-02-09 02:20

    UISearchBar has the round curve, and it is fix if you like to make it rectangle you have to use custom search bar with rectangular image as like of search bar and 1 uibutton and UItextfield, and your own searching logic

    0 讨论(0)
提交回复
热议问题