A Swift implementation for LTNavigationBar

来源:互联网 发布:游戏交易网站源码 编辑:程序博客网 时间:2024/05/16 16:57

// A Swift implementation for LTNavigationBar

extension UINavigationBar {    private struct AssociatedKeys {        static var overlayKey = "overlayKey"        static var emptyImageKey = "emptyImageKey"    }    var overlay: UIView? {        get {            return objc_getAssociatedObject(self, &AssociatedKeys.overlayKey) as? UIView        }        set {            objc_setAssociatedObject(self, &AssociatedKeys.overlayKey, newValue, UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC))        }    }    var emptyImage: UIImage? {        get {            return objc_getAssociatedObject(self, &AssociatedKeys.emptyImageKey) as? UIImage        }        set {            objc_setAssociatedObject(self, &AssociatedKeys.emptyImageKey, newValue, UInt(OBJC_ASSOCIATION_RETAIN_NONATOMIC))        }    }    func lt_setBackgroundColor(backgroundColor: UIColor?) {        if var view = self.overlay {            self.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)            view = UIView(frame: CGRect(x: 0, y: -20, width: UIScreen.mainScreen().bounds.width, height: CGRectGetHeight(self.bounds) + 20))            view.userInteractionEnabled = false            view.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight            self.insertSubview(view, atIndex: 0)        }        self.overlay?.backgroundColor = backgroundColor    }    func lt_setTranslationY(translationY: CGFloat) {        self.transform = CGAffineTransformMakeTranslation(0, translationY)    }    func lt_setContentAlpha(alpha: CGFloat) {        if self.overlay == nil {            self.lt_setBackgroundColor(self.barTintColor)        }        self.setAlpha(alpha, forSubviewsOfView: self)        if alpha == 1 {            if self.emptyImage == nil {                self.emptyImage = UIImage()            }            self.backIndicatorImage = self.emptyImage        }    }    func setAlpha(aplha: CGFloat, forSubviewsOfView view: UIView) {        for subview in view.subviews {            if let view = subview as? UIView {                if view == self.overlay {                    continue                }                view.alpha = alpha                self.setAlpha(alpha, forSubviewsOfView: view)            }        }    }    func lt_reset() {        self.setBackgroundImage(nil, forBarMetrics: UIBarMetrics.Default)        self.shadowImage = nil        self.overlay?.removeFromSuperview()        self.overlay = nil    }}
0 0
原创粉丝点击