57.TabBar和NVBar的背景和阴影颜色设置

2018-09-21  本文已影响12人  noonez
class CustomTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        //设置tabbar的背景和阴影图片
        tabBar.backgroundImage = UIImage()
        tabBar.shadowImage = drawShadowLine(height: 1, color: UIColor.green)
    }
    
    
    func drawShadowLine(height:CGFloat, color:UIColor)->UIImage? {
        UIGraphicsBeginImageContextWithOptions(CGSize(width: UIScreen.main.bounds.width, height: height), false, UIScreen.main.scale)
        if let context = UIGraphicsGetCurrentContext() {
            context.setFillColor(color.cgColor)
            context.fill(CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: height))
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return image
        }
        return nil
    }
}
class CustomNVController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        navigationBar.setBackgroundImage(UIImage(), for: .default)
        navigationBar.shadowImage = drawShadowLine(height: 1, color: UIColor.red)
    }
    
    func drawShadowLine(height:CGFloat, color:UIColor)->UIImage? {
        UIGraphicsBeginImageContextWithOptions(CGSize(width: UIScreen.main.bounds.width, height: height), false, UIScreen.main.scale)
        if let context = UIGraphicsGetCurrentContext() {
            context.setFillColor(color.cgColor)
            context.fill(CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: height))
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return image
        }
        return nil
    }

}
上一篇下一篇

猜你喜欢

热点阅读