使用Swift在iOS 8.1中自定义UISearchBar

时间:2015-01-06 17:07:26

标签: swift ios8.1

我想在UISearchBar中自定义以下内容,如果有可能的话,那么:

  • 默认情况下,更改搜索图标的图标颜色。
  • 将图标的方向更改为向右而不是向左。
  • 更改插入的文字的字体大小和字体类型以查找。

下图是使用SDK相同图标的两个第一个问题的示例。

enter image description here

提前致谢。

PD:请仅参考Swift。

1 个答案:

答案 0 :(得分:0)

  • SWIFT
  • IOS 8

1)找到一个好的图标,你想要的颜色,并使用.setImage

设置它
// images
self.setImage(UIImage(named: "Search-white"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal)

3)对我来说,这使用递归函数来查找实际的文本字段。

extension UIView {

func recursive_applyTheme_Search(
    #dynamicTextStyle: NSString,
    bgColor: UIColor,
    cursorColor: UIColor,
    textColor: UIColor,
    placeholderTextColor: UIColor,
    borderColor: UIColor,
    borderWidth: CGFloat,
    cornerRadius: CGFloat) {

    for subview in self.subviews
    {
        if subview is UITextField {

            (subview as! UITextField).applyThemeForSearchBar(
                dynamicTextStyle: dynamicTextStyle,
                bgColor: bgColor,
                cursorColor: cursorColor,
                textColor: textColor,
                placeholderTextColor: placeholderTextColor,
                borderColor: borderColor,
                borderWidth: borderWidth,
                cornerRadius: cornerRadius)
        }
        else { subview.recursive_applyTheme_Search(
                dynamicTextStyle: dynamicTextStyle,
                bgColor: bgColor,
                cursorColor: cursorColor,
                textColor: textColor,
                placeholderTextColor: placeholderTextColor,
                borderColor: borderColor,
                borderWidth: borderWidth,
                cornerRadius: cornerRadius) }
    }

}
}