Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.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 如何使UISearchBar看起来像这样?_Ios_Swift_Customization_Uisearchbar - Fatal编程技术网

Ios 如何使UISearchBar看起来像这样?

Ios 如何使UISearchBar看起来像这样?,ios,swift,customization,uisearchbar,Ios,Swift,Customization,Uisearchbar,我希望在我的应用程序中有这样一个搜索栏: 正如您所看到的,在不编辑时,右边应该有放大镜,在编辑时,右边应该有清除按钮。没有取消按钮 我通过子类化UISearchBar创建了CustomSearchBar类: import UIKit class CustomSearchBar: UISearchBar { override func layoutSubviews() { super.layoutSubviews() customize()

我希望在我的应用程序中有这样一个搜索栏:

正如您所看到的,在不编辑时,右边应该有放大镜,在编辑时,右边应该有清除按钮。没有取消按钮

我通过子类化UISearchBar创建了CustomSearchBar类:

import UIKit

class CustomSearchBar: UISearchBar {

    override func layoutSubviews() {
        super.layoutSubviews()

        customize()
    }

    private func customize() {
        setShowsCancelButton(false, animated: false)

        placeholder = nil
        barTintColor = UIColor.whiteColor()
        tintColor = UIColor.blackColor()

        let textField = getTextField()
        textField.backgroundColor = UIColor.clearColor();
        textField.font = textField.font?.fontWithSize(18.0)

        textField.leftViewMode = .UnlessEditing
    }

}
和UISearchController:

import UIKit

class CustomSearchController: UISearchController, UISearchBarDelegate {

    lazy var _searchBar: CustomSearchBar = {
        [unowned self] in
        let result = CustomSearchBar(frame: CGRectZero)
        result.delegate = self

        return result
    }()

    override var searchBar: UISearchBar {
        get {
            return _searchBar
        }
    }

}
这样,我得到了适当的字体大小,背景颜色和没有取消按钮

问题是,我不知道如何让这个放大镜在不编辑的情况下保持在文本字段的右侧。我试过了,但没有成功。另一个问题是,当我使用
setShowsCancelButton(false,动画:false)
使取消按钮消失时,它也会使文本光标消失


有没有可能像实物模型上那样有更大的清晰和放大镜图标?

您可以使用reactivecocoa自定义它。这样做很容易,我自定义了一个更困难的控件。您可以使用reactivecocoa自定义它。这样做很容易,我自定义了一个更困难的控件。