如何使用@IBDesignables自定义UIBarButtonItem?

时间:2016-06-22 09:39:47

标签: ios xcode swift

@IBDesignable class AttributedBarButtonItem: UIBarButtonItem {

    @IBInspectable var fontSize: CGFloat = 22 {

        didSet {

            let attributes: [String: AnyObject] = [NSFontAttributeName: UIFont(name: "Icomoon", size: fontSize)!]
            setTitleTextAttributes(attributes, forState: .Normal)
        }
    }
}

这是属性检查器

中的样子

enter image description here

但它没有改变任何东西:

enter image description here

3 个答案:

答案 0 :(得分:4)

UIKeyboardWillShowNotification无法与frame一起使用,因为setScale仅适用于BigDecimal(19.1234).setScale(2, BigDecimal.RoundingMode.DOWN) => res: scala.math.BigDecimal = 19.12 个子类(在iOS上),@IBDesignable不是UIBarButtonItem子类。

答案 1 :(得分:1)

只能通过UINavigationBar

进行自定义
@IBDesignable class AttributedNavigationBar: UINavigationBar {

    @IBInspectable var transparentBackground: Bool = false {

        didSet {

            setBackgroundImage(transparentBackground ? UIImage() : nil, forBarMetrics: .Default)
            shadowImage = transparentBackground ? UIImage() : nil
        }
    }

    @IBInspectable var fontSize: CGFloat = 18
    @IBInspectable var icomoonLeftBarButtonItems: Bool = false {

        didSet {

            let attributes = [NSFontAttributeName: UIFont(name: "Icomoon", size: fontSize)!]

            if let leftBarButtonItems = topItem?.leftBarButtonItems {

                for leftBarButtonItem in leftBarButtonItems {
                    leftBarButtonItem.setTitleTextAttributes(icomoonLeftBarButtonItems ? attributes : nil, forState: .Normal)
                }
            }
        }
    }
}

像魅力一样。

答案 2 :(得分:0)

试试这个:

fn-SubString