ios - UIButton 底部阴影

我有一个与标准 iOS 键盘字母按钮非常相似的 UIButton

我不确定如何像 iOS 那样只为底层创建阴影。

我使用下面的代码,但我看到所有侧面都有阴影,而不仅仅是底部:

CALayer *buttonLayer = [[CALayer alloc] init];
buttonLayer.shadowColor = [UIColor grayColor].CGColor;
buttonLayer.shadowOffset = CGSizeMake(0.f,1.f);
buttonLayer.masksToBounds = NO;
buttonLayer.shadowOpacity = 1.f;

你能告诉我如何达到同样的效果吗?提前致谢。

最佳答案

您可以混合使用cornerRadius 和shadow 属性。我在 iOS 8 上测试过。

objective-C :

[self.globeButton setImage:[UIImage imageNamed:@"Globe"] forState:UIControlStateNormal];
self.globeButton.backgroundColor = [UIColor colorWithRed:171 green:178 blue:186 alpha:1.0f];
// Shadow and Radius
self.globeButton.layer.shadowColor = [[UIColor colorWithRed:0 green:0 blue:0 alpha:0.25f] CGColor];
self.globeButton.layer.shadowOffset = CGSizeMake(0, 2.0f);
self.globeButton.layer.shadowOpacity = 1.0f;
self.globeButton.layer.shadowRadius = 0.0f;
self.globeButton.layer.masksToBounds = NO;
self.globeButton.layer.cornerRadius = 4.0f;

swift :

globeButton.setImage(UIImage(named: "Globe"), for: .normal)
globeButton.backgroundColor = UIColor(red: 171/255, green: 178/255, blue: 186/255, alpha: 1.0)
// Shadow Color and Radius
globeButton.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25).cgColor
globeButton.layer.shadowOffset = CGSize(width: 0.0, height: 2.0)
globeButton.layer.shadowOpacity = 1.0
globeButton.layer.shadowRadius = 0.0
globeButton.layer.masksToBounds = false
globeButton.layer.cornerRadius = 4.0

结果:

https://stackoverflow.com/questions/27076419/

相关文章:

iphone - 如何设置字体大小以填充 UILabel 高度?

objective-c - 如何在 Objective-C 中使用自定义委托(delegate)

iphone - 如何在 UITextView 中设置边距(填充)?

objective-c - 对 NSTimer 目标的弱引用以防止保留循环

objective-c - iOS 8 Mapkit Objc 无法获取用户位置

objective-c - 如何在 Objective-C 中调用方法?

ios - 如何调整 UITableView 中原型(prototype)单元格的左边距?

objective-c - Objective-C 中的简单 http post 示例?

objective-c - 可变集合有文字语法吗?

iphone - 如何阻止 NSNotification 中的观察者调用两次?