                 objc_setAssociatedObject(self, &AssociatedKeys.kFont, value, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
                 if (placeholder != nil) {
                     // 触发 setPlaceholder 里更新 placeholder 样式的逻辑
-                    //                    self.placeholder = self.placeholder
+                    let holder = placeholder
+                    placeholder = holder
                 }
                 
                 // 更新输入框的文字样式
-                self.qmui_textField().font = value;
-            }
-        }
-    }
-    
-    public func qmui_textField() -> UITextField {
-        let textField = value(forKey: "searchField") as! UITextField
-        return textField
+                qmui_textField()?.font = value;
+            }
+        }
+    }
+    
+    public func qmui_textField() -> UITextField? {
+        if let textField = value(forKey: "searchField") {
+             return textField as? UITextField
+        }
+        return nil
+    }
+    
+    public var qmui_textFieldMargins: UIEdgeInsets {
+        get {
+            return objc_getAssociatedObject(self, &AssociatedKeys.kTextFieldMargins) as? UIEdgeInsets ?? UIEdgeInsets.zero
+        }
+        set {
+            objc_setAssociatedObject(self, &AssociatedKeys.kTextFieldMargins, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
+        }
+    }
+    
+    public func qmui_cancelButton() -> UIButton? {
+        if let button = value(forKey: "cancelButton") {
+            return button as? UIButton
+        }
+        return nil
+    }
+    
+    public func qmui_segmentedControl() -> UISegmentedControl? {
+        // 注意，segmentedControl 只是整条 scopeBar 里的一部分，虽然它的 key 叫做“scopeBar”
+        if let segmentedControl = value(forKey: "scopeBar") {
+            return segmentedControl as? UISegmentedControl
+        }
+        return nil
+    }
+    
+    public func qmui_backgroundView() -> UIView? {
+        if let backgroundView = value(forKey: "background") {
+            return backgroundView as? UIView
+        }
+        return nil
     }
 
-    func qmui_styledAsQMUISearchBar() {
-        //        self.qmui_textColor = SearchBarTextColor;
-        //        self.qmui_placeholderColor = SearchBarPlaceholderColor;
-        //        self.placeholder = @"搜索";
-        //        self.autocorrectionType = UITextAutocorrectionTypeNo;
-        //        self.autocapitalizationType = UITextAutocapitalizationTypeNone;
-        //        self.searchTextPositionAdjustment = UIOffsetMake(5, 0);
-        //
-        //        // 设置搜索icon
-        //        UIImage *searchIconImage = SearchBarSearchIconImage;
-        //        if (searchIconImage) {
-        //            if (!CGSizeEqualToSize(searchIconImage.size, CGSizeMake(13, 13))) {
-        //                QMUILog(@"搜索框放大镜图片（SearchBarSearchIconImage）的大小最好为 (13, 13)，否则会失真，目前的大小为 %@", NSStringFromCGSize(searchIconImage.size));
-        //            }
-        //            [self setImage:searchIconImage forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
-        //        }
-        //
-        //        // 设置搜索右边的清除按钮的icon
-        //        UIImage *clearIconImage = SearchBarClearIconImage;
-        //        if (clearIconImage) {
-        //            [self setImage:clearIconImage forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];
-        //        }
-        //
-        //        // 设置SearchBar上的按钮颜色
-        //        self.tintColor = SearchBarTintColor;
-        //
-        //        // 输入框背景图
-        //        UIColor *textFieldBackgroundColor = SearchBarTextFieldBackground;
-        //        if (textFieldBackgroundColor) {
-        //            [self setSearchFieldBackgroundImage:[[[UIImage qmui_imageWithColor:SearchBarTextFieldBackground size:CGSizeMake(60, 28) cornerRadius:SearchBarTextFieldCornerRadius] qmui_imageWithBorderColor:SearchBarTextFieldBorderColor borderWidth:PixelOne cornerRadius:SearchBarTextFieldCornerRadius] resizableImageWithCapInsets:UIEdgeInsetsMake(14, 14, 14, 14)] forState:UIControlStateNormal];
-        //        }
-        //
-        //        // 整条bar的背景
-        //        // iOS7及以后不用barTintColor设置背景是因为这么做的话会出现上下border，去不掉，所以iOS6和7都改为用backgroundImage实现
-        //        UIColor *barTintColor = SearchBarBarTintColor;
-        //        if (barTintColor) {
-        //            UIImage *backgroundImage = [[[UIImage qmui_imageWithColor:SearchBarBarTintColor size:CGSizeMake(10, 10) cornerRadius:0] qmui_imageWithBorderColor:SearchBarBottomBorderColor borderWidth:PixelOne borderPosition:QMUIImageBorderPositionBottom] resizableImageWithCapInsets:UIEdgeInsetsMake(1, 1, 1, 1)];
-        //            [self setBackgroundImage:backgroundImage forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
-        //        }
+    public func qmui_styledAsQMUISearchBar() {
+        // 搜索框的字号及 placeholder 的字号
+        if let font = SearchBarFont {
+            qmui_font = font
+        }
+        
+        // 搜索框的文字颜色
+        if let textColor = SearchBarTextColor {
+            qmui_textColor = textColor
+        }
+        
+        // placeholder 的文字颜色
+        if let placeholderColor = SearchBarPlaceholderColor {
+            qmui_placeholderColor = placeholderColor
+        }
+        
+        placeholder = "搜索"
+        autocorrectionType = .no
+        autocapitalizationType = .none
+        searchTextPositionAdjustment = UIOffset(horizontal: 5, vertical: 0)
+        
+        // 设置搜索icon
+        if let searchIconImage = SearchBarSearchIconImage {
+            if (!searchIconImage.size.equalTo(CGSize(width: 13, height: 13))) {
+                QMUILog("搜索框放大镜图片（SearchBarSearchIconImage）的大小最好为 (13, 13)，否则会失真，目前的大小为 \(NSStringFromCGSize(CGSize(width: 13, height: 23)))")
+            }
+            setImage(searchIconImage, for: .search, state: .normal)
+        }
+        
+        // 设置搜索右边的清除按钮的icon
+        if let clearIconImage = SearchBarClearIconImage {
+            setImage(clearIconImage, for: .clear, state: .normal)
+        }
+        
+        // 设置SearchBar上的按钮颜色
+        tintColor = SearchBarTintColor
+        
+        // 输入框背景图
+        if let textFieldBackgroundColor = SearchBarTextFieldBackground {
+            var image = UIImage.qmui_image(withColor: textFieldBackgroundColor, size: CGSize(width: 60, height: 28), cornerRadius: SearchBarTextFieldCornerRadius)
+            image = image?.qmui_image(withBorderColor: textFieldBackgroundColor, borderWidth: PixelOne, cornerRadius: SearchBarTextFieldCornerRadius)
+            image?.resizableImage(withCapInsets: UIEdgeInsets(top: 14, left: 14, bottom: 14, right: 14))
+            setSearchFieldBackgroundImage(image, for: .normal)
+        }
+        
+        // 整条bar的背景
+        // 为了让 searchBar 底部的边框颜色支持修改，背景色不使用 barTintColor 的方式去改，而是用 backgroundImage
+        var backgroundImage = UIImage.qmui_image(withColor: SearchBarBarTintColor, size: CGSize(width: 10, height: 10), cornerRadius: 0)
+        
+        if (backgroundImage == nil) {
+            backgroundImage = UIImage.qmui_image(withColor: .white, size: CGSize(width: 10, height: 10), cornerRadius: 0)
+        }
+        backgroundImage = backgroundImage?.qmui_image(withBorderColor: SearchBarBottomBorderColor, borderWidth: PixelOne, borderPosition: .bottom)
+        
+        if (backgroundImage != nil) {
+            backgroundImage = backgroundImage?.resizableImage(withCapInsets: UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1))
+            setBackgroundImage(backgroundImage, for: .any, barMetrics: .default)
+            setBackgroundImage(backgroundImage, for: .any, barMetrics: .defaultPrompt)
+        }
     }
 }

    static func awake() {
        DispatchQueue.once(token: _onceToken) {
            let selectors = [
                #selector(setter: placeholder)
            ]
            selectors.forEach({
                print("qmui_" + $0.description)

                objc_setAssociatedObject(self, &AssociatedKeys.kPlaceholderColor, value, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
                if (placeholder != nil) {
                    // 触发 setPlaceholder 里更新 placeholder 样式的逻辑
                    let holder = placeholder
                    placeholder = holder
                }
            }
        }
    }
    
    public var qmui_textColor: UIColor? {
        get {
            return objc_getAssociatedObject(self, &AssociatedKeys.kTextColor) as? UIColor
        }
        set {
            if let value = newValue {
                objc_setAssociatedObject(self, &AssociatedKeys.kTextColor, value, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
                qmui_textField()?.textColor = value
            }
        }
    }
