How to add a drop shadow to a UIButton?(如何向 UIButton 添加阴影?)
问题描述
我想为 UIButton 添加阴影.我尝试使用 self.layer.shadow* 属性.这些属性在 UIView 中有效,但在 UIButton 中的行为不同.如果我能得到任何指示来绘制阴影,我将不胜感激.谢谢!
I would like to add a drop shadow to a UIButton. I tried to use self.layer.shadow* properties. Those properties work in UIView, but they behave differently in UIButton. I would really appreciate it if I could get any pointers to draw the drop shadow. Thank you!
self.layer.cornerRadius = 8.0f;
self.layer.masksToBounds = YES;
self.layer.borderWidth = 1.0f;
self.layer.shadowColor = [UIColor greenColor].CGColor;
self.layer.shadowOpacity = 0.8;
self.layer.shadowRadius = 12;
self.layer.shadowOffset = CGSizeMake(12.0f, 12.0f);
推荐答案
问题中只有一个小错误导致阴影不显示:masksToBounds:YES
也屏蔽了阴影!这是正确的代码:
There is only one tiny mistake in the question that causes the shadow to not be displayed: masksToBounds:YES
also masks the shadow! Here's the correct code:
self.layer.cornerRadius = 8.0f;
self.layer.masksToBounds = NO;
self.layer.borderWidth = 1.0f;
self.layer.shadowColor = [UIColor greenColor].CGColor;
self.layer.shadowOpacity = 0.8;
self.layer.shadowRadius = 12;
self.layer.shadowOffset = CGSizeMake(12.0f, 12.0f);
不幸的是,这意味着我们不能在没有技巧的情况下同时屏蔽内容和阴影.
Unfortunately, this means we cannot mask the content and have a shadow at the same time without tricks.
记得#import <QuartzCore/QuartzCore.h>
.
这篇关于如何向 UIButton 添加阴影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何向 UIButton 添加阴影?
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01