How to change UISearchBar from round to rectangle?

I will be interested to know how I need to change the UISearchBar from the default round curve to a rectangle.

enter image description here

enter image description here

+8
ios iphone uisearchbar
source share
12 answers
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 } } } 
+19
source share

Just add the background image, left image and right view for your text box.

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]; 
+2
source share

UISearchBar has a circular curve, and the correction, if you want to make it a rectangle, you need to use the user search panel with a rectangular image, as in the search bar and 1 uibutton and UItextfield, as well as its own logical logic

+2
source share

Much better, in my opinion:

 UITextField *txfSearchField = [_searchBar valueForKey:@"_searchField"]; txfSearchField.borderStyle = UITextBorderStyleNone; 
+2
source share

I would use UIView , with these functions:
- set the frame.
- import CoreGraphics into your class
- set the white background of the view
- set view.layer.cornerRadius=10;
- set view.layer.borderWidth=8;
- set view.layer.borderColor=[UIColor blackColor].CGColor;
- insert a shortcut (with a clear background) inside the created view
For the button on the left, I would use uiimage, for the right button, set textField.clearButtonMode= UITextFieldViewModeAlways;

Hope this helps.

+1
source share

In the target image, I do not see the default shadow, so I mentioned that setting the background property to nil will remove it. I needed to do this in my project, and delete the image and manipulate the border well. I had to drop the control to get the background property.

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

@Keller thanks for the tip for finding the control.

+1
source share

In our previous project, we also tried to implement in this way, but configuration is not possible.

0
source share

Could you try this.

  for (UIView *searchBarSubview in [search_bar subviews]) { if ([searchBarSubview conformsToProtocol:@protocol(UITextInputTraits)]) { if([searchBarSubview isKindOfClass:[UITextField class]]) { [(UITextField *)searchBarSubview setBorderStyle:UITextBorderStyleRoundedRect]; } } } 
0
source share

I also recently achieved this in a project by going through the areas of the UISearchBar object. However, the text box was not an immediate child, as the accepted answer suggested, but a peek at another peep. At some point, the hierarchy of the internal appearance was probably changed. (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 undocumented methods, so it is probably safe on the App Store, although it may stop working with future changes in the SDK.

0
source share

Why not use the UIAppearance protocol?

[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setBorderStyle:UITextBorderStyleRoundedRect];

0
source share

Swift 3

  let textFieldInsideUISearchBar = searchBar.value(forKey: "searchField") as? UITextField textFieldInsideUISearchBar?.borderStyle = .none textFieldInsideUISearchBar?.backgroundColor = UIColor.white 
0
source share

The way it worked with me on Swift 4 is to subclass the UISearchBar and make the necessary adjustments there without any hacking or workaround ... I used the subclass used in this tutorial and it worked like a charm

Here is the code from the example used

 // // SearchBar.swift // Yomu // // Created by Sendy Halim on 9/3/17. // Copyright © 2017 Sendy Halim. All rights reserved. // import Foundation import UIKit class SearchBar: UISearchBar { override func willMove(toSuperview newSuperview: UIView?) { super.willMove(toSuperview: newSuperview) searchBarStyle = .minimal // Create search icon let searchIcon = UIImageView(image: #imageLiteral(resourceName: "search")) let searchImageSize = searchIcon.image!.size searchIcon.frame = CGRect(x: 0, y: 0, width: searchImageSize.width + 10, height: searchImageSize.height) searchIcon.contentMode = UIViewContentMode.center // Configure text field let textField = value(forKey: "_searchField") as! UITextField textField.leftView = searchIcon textField.borderStyle = .none textField.backgroundColor = UIColor(hex: "#F7F7F7") textField.clipsToBounds = true textField.layer.cornerRadius = 6.0 textField.layer.borderWidth = 1.0 textField.layer.borderColor = textField.backgroundColor!.cgColor textField.textColor = UIColor(hex: "#555555") } } 
0
source share

All Articles