gpt4 book ai didi

ios - UIButton 底部阴影

转载 作者:IT老高 更新时间:2023-10-28 11:33:45 27 4
gpt4 key购买 nike

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

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

enter image description here

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

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

结果:

UIButton + iOS Keyboard style

关于ios - UIButton 底部阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27076419/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com