我刚刚放置了一个带有表视图的搜索栏控制器。现在它看起来像这样的普通搜索栏:
像圆形和搜索图标应该在最后显示,当我按下搜索时,应自动显示取消按钮。
现在通过使用代码,是否可以进行这些更改。请帮帮我。如何得到上面的图像。
我正在使用swift 2.0
答案 0 :(得分:0)
将UIView作为textField的containerView,根据需要设置背景颜色。
将角半径设置为textField并为其添加一些填充。
转弯半径:
<强> 夫特: 强>
let textFieldSearchBar = UITextField()
textFieldSearchBar.layer.masksToBounds = true
textFieldSearchBar.layer.cornerRadius = 5.2
textFieldSearchBar.layer.borderColor = UIColor.lightGrayColor().CGColor
textFieldSearchBar.layer.borderWidth = 1.5
<强> 目标C: 强>
textFieldSearchBar.layer.cornerRadius = 5.2f;
textFieldSearchBar.layer.borderColor = kTextFieldBorderColor.CGColor;
textFieldSearchBar.layer.borderWidth = 1.5f;
左边填充:
<强> 夫特: 强>
let leftPaddingView = UIView(frame: CGRect(x: 0, y: 0, width: paddingWidth, height: 20))
textFieldSearchBar.leftView = leftPaddingView
textFieldSearchBar.leftViewMode = UITextFieldViewMode.Always;
<强> 目标C: 强>
UIView *leftPaddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, paddingWidth, 20)];
textFieldSearchBar.leftView = leftPaddingView;
textFieldSearchBar.leftViewMode = UITextFieldViewModeAlways;
右边填充:
<强> 夫特: 强>
let rightPaddingView = UIView(frame: CGRect(x: 0, y: 0, width: paddingWidth, height: 20))
textFieldSearchBar.rightView = rightPaddingView
textFieldSearchBar.rightViewMode = UITextFieldViewMode.Always;
<强> 目标C: 强>
UIView *rightPaddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 20)];
textFieldSearchBar.rightView = rightPaddingView;
textFieldSearchBar.rightViewMode = UITextFieldViewModeAlways;
<强> 夫特: 强>
buttonSearch.setBackgroundImage(UIImage(named: "magnifyingGlass"), forState: UIControlState.Normal)
buttonSearch.setBackgroundImage(UIImage(named: "crossButton"), forState: UIControlState.Selected)
//Show magnifying glass image
buttonSearch.selected = false
//Show cross button image
buttonSearch.selected = true
我创建了自定义搜索bas,如下所示:
希望这会对你有所帮助。