美文网首页
导航栏渐变的实现

导航栏渐变的实现

作者: 羊驼先生丶 | 来源:发表于2017-03-27 16:16 被阅读80次

    建立分类

    import UIKit
    
    
    extension UINavigationBar {
        private struct AssociatedKeys {
            static var overlayKey = "overlayKey"
        }
        //使用runtime添加属性
        var overlay: UIView? {
            get {
                return objc_getAssociatedObject(self, &AssociatedKeys.overlayKey) as? UIView
            }
            set {
                objc_setAssociatedObject(self, &AssociatedKeys.overlayKey, newValue as UIView?, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC
                )
            }
        }
    }
    
    
    extension UINavigationBar {
        //改变导航栏的颜色
        func lt_setBackgroundColor(backgroundColor: UIColor) {
            if overlay == nil {
                self.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
                overlay = UIView.init(frame: CGRect.init(x: 0, y: 0, width: bounds.width, height: bounds.height+20))
                overlay?.isUserInteractionEnabled = false
                overlay?.autoresizingMask = UIViewAutoresizing.flexibleWidth
                subviews.first?.insertSubview(overlay!, at: 0)
            }
            overlay?.backgroundColor = backgroundColor
        }
        
        //动画的形式消失,显得不那么突兀
        func lt_setTranslationY(translationY: CGFloat) {
            transform = CGAffineTransform.init(translationX: 0, y: translationY)
        }
        
        //让子视图消失达到隐藏导航栏的作用
        func lt_setElementsAlpha(alpha: CGFloat) {
            for (_, element) in subviews.enumerated() {
                if element.isKind(of: NSClassFromString("UINavigationItemView") as! UIView.Type) ||
                    element.isKind(of: NSClassFromString("UINavigationButton") as! UIButton.Type) ||
                    element.isKind(of: NSClassFromString("UINavBarPrompt") as! UIView.Type)
                {
                    element.alpha = alpha
                }
                
                if element.isKind(of: NSClassFromString("_UINavigationBarBackIndicatorView") as! UIView.Type) {
                    element.alpha = element.alpha == 0 ? 0 : alpha
                }
            }
            
            items?.forEach({ (item) in
                if let titleView = item.titleView {
                    titleView.alpha = alpha
                }
                for BBItems in [item.leftBarButtonItems, item.rightBarButtonItems] {
                    BBItems?.forEach({ (barButtonItem) in
                        if let customView = barButtonItem.customView {
                            customView.alpha = alpha
                        }
                    })
                }
            })
        }
        
        
        func lt_reset() {
            setBackgroundImage(nil, for: UIBarMetrics.default)
            overlay?.removeFromSuperview()
            overlay = nil
        }
    }
    

    https://github.com/ltebean/LTNavigationBar/tree/swift3.0

    相关文章

      网友评论

          本文标题:导航栏渐变的实现

          本文链接:https://www.haomeiwen.com/subject/kgurottx.html