iOS - 在 UITabBar 上方获得所需的阴影
iOS - Getting desired shadow above UITabBar
我正在尝试让我的标签栏阴影看起来像这张图片中看到的那样:
最好的方法是什么?我正在使用 objective-c
谢谢
试试这个
[[UITabBar appearance] setShadowImage:[UIImage imageNamed:@"transparentShadow.png"]];
您可以使用以下代码为任何 UI 对象赋予阴影
tabBar.layer.shadowOffset = CGSize(width: 0, height: 0)
tabBar.layer.shadowRadius = 2
tabBar.layer.shadowColor = UIColor.black.cgColor
tabBar.layer.shadowOpacity = 0.3
这里我给出了你的 tabControl 对象的例子。
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
我更愿意使用专用的标签栏方法。
// Set `backgroundImage` to be able to use `shadowImage`
tabBar.backgroundImage = UIImage.imageWithColor(.white)
tabBar.shadowImage = #imageLiteral(resourceName: "tab_bar_shadow") // 2x34pt works for me
对于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
Swift 4:
使用此扩展程序
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!
}
}
使用 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))
self.tabBarController.tabBar.shadowImage = [[UIImage alloc] init];
self.tabBarController.tabBar.backgroundImage = [[UIImage alloc] init];
self.tabBarController.tabBar.backgroundColor = [UIColor whiteColor];
self.tabBarController.tabBar.layer.shadowOffset = CGSizeMake(0, 0);
self.tabBarController.tabBar.layer.shadowRadius = 1;
self.tabBarController.tabBar.layer.shadowColor = [UIColor blackColor].CGColor;
self.tabBarController.tabBar.layer.shadowOpacity = 0.2;
我正在尝试让我的标签栏阴影看起来像这张图片中看到的那样:
最好的方法是什么?我正在使用 objective-c
谢谢
试试这个
[[UITabBar appearance] setShadowImage:[UIImage imageNamed:@"transparentShadow.png"]];
您可以使用以下代码为任何 UI 对象赋予阴影
tabBar.layer.shadowOffset = CGSize(width: 0, height: 0)
tabBar.layer.shadowRadius = 2
tabBar.layer.shadowColor = UIColor.black.cgColor
tabBar.layer.shadowOpacity = 0.3
这里我给出了你的 tabControl 对象的例子。
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
我更愿意使用专用的标签栏方法。
// Set `backgroundImage` to be able to use `shadowImage`
tabBar.backgroundImage = UIImage.imageWithColor(.white)
tabBar.shadowImage = #imageLiteral(resourceName: "tab_bar_shadow") // 2x34pt works for me
对于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
Swift 4:
使用此扩展程序
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!
}
}
使用 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))
self.tabBarController.tabBar.shadowImage = [[UIImage alloc] init];
self.tabBarController.tabBar.backgroundImage = [[UIImage alloc] init];
self.tabBarController.tabBar.backgroundColor = [UIColor whiteColor];
self.tabBarController.tabBar.layer.shadowOffset = CGSizeMake(0, 0);
self.tabBarController.tabBar.layer.shadowRadius = 1;
self.tabBarController.tabBar.layer.shadowColor = [UIColor blackColor].CGColor;
self.tabBarController.tabBar.layer.shadowOpacity = 0.2;