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

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
    }

}

你可能感兴趣的:(57.TabBar和NVBar的背景和阴影颜色设置)