如何将UISearchBar从圆形更改为矩形?

8
我想知道如何将默认的圆形UISearchBar更改为矩形。请参考以下图片: enter image description here enter image description here
12个回答

19
    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
    }
  }
}

然后,您可以使用searchBarTextField.layer.cornerRadius = 5.0;来控制圆角半径。 - esbenr
如果 ([searchBarSubview respondsToSelector:@sel(setBorderStyle)]) [(UITextField *)searchBarSubview setBorderStyle:UITextBorderStyleRoundedRect]; - Joe Masilotti
现在的话,我可能会使用[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setBorderStyle:UITextBorderStyleRoundedRect]; - mattsson
那个解决方案在应用商店安全吗? - pedroremedios

3
在我看来,这个更好:
UITextField *txfSearchField = [_searchBar valueForKey:@"_searchField"];
txfSearchField.borderStyle = UITextBorderStyleNone;

2

为您的TextField添加背景图、左侧视图和右侧视图非常简单。

下面是一个示例:

UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 10., nameField.bounds.size.height)];
nameField.leftView = leftView;
nameField.leftViewMode = UITextFieldViewModeAlways;
leftView release];

2

UISearchBar有圆形曲线,如果你想要将其变成矩形,你需要使用自定义搜索栏,并使用矩形图像(类似于搜索栏)、1个UIButton和UITextField,以及你自己的搜索逻辑。


1
为什么不使用UIAppearance协议?
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setBorderStyle:UITextBorderStyleRoundedRect];

1
在目标图像中,我没有看到默认阴影,所以我会提到将 background 属性设置为 nil 将其移除。我在我的项目中需要这样做,并且删除图像并操作边框效果很好。我必须将控件转换才能访问 background 属性。
UITextField * x = (UITextField*)searchBarSubview;
x.background = nil;

@Keller,感谢您提供寻找控件的提示。


1
我会使用一个带有以下特点的UIView
- 设置你的框架。
- 在你的类中导入CoreGraphics
- 设置视图的白色背景
- 设置view.layer.cornerRadius=10;
- 设置view.layer.borderWidth=8;
- 设置view.layer.borderColor=[UIColor blackColor].CGColor;
- 在创建的视图内插入一个带有透明背景的标签
对于左侧的按钮,我会使用一个uiimage,对于右侧的按钮,设置textField.clearButtonMode= UITextFieldViewModeAlways;

希望这可以帮助到您。


看起来很难,我想我会将其保留为原样。如果它对你有用,请让我知道。谢谢 :) - Desmond

0

Swift 3

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

@Pierre.Vriens 哪一行? - Marosdee Uma
@Pierre.Vriens 看一下已接受的评论,无需解释。只需将其复制粘贴到您的UIViewController中即可。 - Marosdee Uma

0

你能试试这个吗。

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

0
在我们之前的项目中,我们也尝试以这种方式实现,但是定制化不可行。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接