如何向UIButton添加投影?

编程入门 行业动态 更新时间:2024-10-07 14:22:06
本文介绍了如何向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添加投影?

本文发布于:2023-11-03 03:58:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554182.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:UIButton

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!