iOS - Getting desired shadow above UITabBar

后端 未结 6 1697
执笔经年
执笔经年 2021-02-12 19:22

I am trying to get my tab bar shadow to look like the one seen in this image:

What is the best way of doing thi

相关标签:
6条回答
  • 2021-02-12 19:58

    Try This one

     [[UITabBar appearance] setShadowImage:[UIImage imageNamed:@"transparentShadow.png"]];
    
    0 讨论(0)
  • 2021-02-12 19:59

    Swift 4:

    tabBar.layer.shadowOffset = CGSize(width: 0, height: 0)
    tabBar.layer.shadowRadius = 2
    tabBar.layer.shadowColor = UIColor.black.cgColor
    tabBar.layer.shadowOpacity = 0.3
    
    0 讨论(0)
  • 2021-02-12 20:05

    For Swift 5 :

    tabBar.layer.shadowOffset = CGSize(width: 0, height: 0)
    tabBar.layer.shadowRadius = 2
    tabBar.layer.shadowColor = UIColor.black.cgColor
    tabBar.layer.shadowOpacity = 0.3
    
    0 讨论(0)
  • 2021-02-12 20:10

    You can give shadow by using following code to any UI object

    tabBar.layer.shadowOffset = CGSize(width: 0, height: 0)
    tabBar.layer.shadowRadius = 2
    tabBar.layer.shadowColor = UIColor.black.cgColor
    tabBar.layer.shadowOpacity = 0.3
    

    Here i gave example for your tabControl object.

    0 讨论(0)
  • 2021-02-12 20:14

    I'd prefer to use a dedicated tab bar methods.

    // Set `backgroundImage` to be able to use `shadowImage`
    tabBar.backgroundImage = UIImage.imageWithColor(.white)
    tabBar.shadowImage = #imageLiteral(resourceName: "tab_bar_shadow") // 2x34pt works for me
    
    0 讨论(0)
  • 2021-02-12 20:14

    Swift 4:

    Use This Extension

    extension UIImage {
    class func colorForNavBar(color: UIColor) -> UIImage {
        //let rect = CGRectMake(0.0, 0.0, 1.0, 1.0)
    
        let rect = CGRect(origin: CGPoint(x: 0,y :0), size: CGSize(width: 1.0, height: 1.0))
    
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
    
        context!.setFillColor(color.cgColor)
        context!.fill(rect)
    
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    
    
         return image!
        } 
    }
    

    Set Shadow Color Using RGB

    //Set BackgroundColor
     UITabBar.appearance().backgroundImage = UIImage.colorForNavBar(color: .white)
    
    //Set Shadow Color
     UITabBar.appearance().shadowImage = UIImage.colorForNavBar(color: UIColor.init(red: 120/255.0, green: 120/255.0, blue: 120/255.0, alpha: 1.0))
    
    0 讨论(0)
提交回复
热议问题