Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将普通搜索栏更改为类似图像的搜索栏_Ios_Iphone_Swift - Fatal编程技术网

Ios 如何将普通搜索栏更改为类似图像的搜索栏

Ios 如何将普通搜索栏更改为类似图像的搜索栏,ios,iphone,swift,Ios,Iphone,Swift,我刚刚放置了一个带有表视图的搜索栏控制器。现在它看起来像普通的搜索栏,如下所示: 但我需要将此搜索栏更改为下图所示的内容: 像圆角和搜索图标应该显示在最后,当我按下搜索,取消按钮应该自动显示 现在,通过使用代码,可以进行这些更改。请帮帮我。如何得到像上面的图片 我正在使用swift 2.0 将UIView作为文本字段的containerView,根据需要设置其背景色 将“拐角半径”设置为文本字段,并为其添加一些填充 角半径: Swift: 目标-c: 左填充: Swift: 目标-c: 右填

我刚刚放置了一个带有表视图的搜索栏控制器。现在它看起来像普通的搜索栏,如下所示:

但我需要将此搜索栏更改为下图所示的内容:

像圆角和搜索图标应该显示在最后,当我按下搜索,取消按钮应该自动显示

现在,通过使用代码,可以进行这些更改。请帮帮我。如何得到像上面的图片

我正在使用swift 2.0

  • 将UIView作为文本字段的containerView,根据需要设置其背景色

  • 将“拐角半径”设置为文本字段,并为其添加一些填充

  • 角半径:

    Swift:

    目标-c:

    左填充:

    Swift:

    目标-c:

    右填充:

    Swift:

    目标-c:

  • 现在您需要两个图像,一个用于放大镜,另一个用于关闭/交叉按钮。按下一个按钮,将这些图像设置为默认和选中状态的背景。 使按钮的出口,现在当你想显示放大镜做[按钮搜索设置选择:否];当交叉按钮执行[按钮搜索设置选择:是] Swift:

    我已创建自定义搜索bas,如下所示:


    希望这对你有帮助

    定制一个:)。改为使用文本字段。uitext字段??如果我放置了任何用于搜索的图像图标,那么当我按下搜索时如何获得取消按钮。你能举个例子吗?谢谢你的回答,如果可能的话,你能为swift 2.0做一个例子吗。因为我是从swift ios 2.0开始的,我已经尝试过这段代码,我在左右填充的
    paddingWidth
    中遇到了一些错误您遇到了什么错误?您是否尝试过提供一些宽度作为UIView*leftPaddingView=[[UIView alloc]initWithFrame:CGRectMake(0,0,20,20)];
        let textFieldSearchBar = UITextField()
        textFieldSearchBar.layer.masksToBounds = true
        textFieldSearchBar.layer.cornerRadius = 5.2
        textFieldSearchBar.layer.borderColor = UIColor.lightGrayColor().CGColor
        textFieldSearchBar.layer.borderWidth = 1.5
    
        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;
    
        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;
    
        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