阴影路径无法使用UIButton
我正在创建一个如下按钮:阴影路径无法使用UIButton,uibutton,swift,calayer,shadow,Uibutton,Swift,Calayer,Shadow,我正在创建一个如下按钮: let authorizationButton = UIButton() let authorizationButtonHeight = 50.0 let authorizationButtonX = 10.0 let authorizationButtonWidth = UIScreen.mainScreen().bounds.size.width - authorizationButtonX * 2 let authorization
let authorizationButton = UIButton()
let authorizationButtonHeight = 50.0
let authorizationButtonX = 10.0
let authorizationButtonWidth = UIScreen.mainScreen().bounds.size.width - authorizationButtonX * 2
let authorizationButtonY = UIScreen.mainScreen().bounds.size.height - 10.0 - authorizationButtonHeight
authorizationButton.frame = CGRectMake(authorizationButtonX, authorizationButtonY, authorizationButtonWidth, authorizationButtonHeight)
authorizationButton.layer.shadowPath = UIBezierPath(rect:CGRectMake(authorizationButton.frame.origin.x, authorizationButton.frame.origin.y,
authorizationButton.frame.size.width, authorizationButton.frame.size.height)).CGPath
authorizationButton.layer.shadowColor = UIColor.blackColor().CGColor
authorizationButton.layer.shadowOpacity = 1.0
authorizationButton.layer.shadowRadius = 3.0
authorizationButton.layer.shadowOffset = CGSizeMake(3.0, 3.0)
之后,我尝试使用自己的阴影路径添加阴影,如下所示:
let authorizationButton = UIButton()
let authorizationButtonHeight = 50.0
let authorizationButtonX = 10.0
let authorizationButtonWidth = UIScreen.mainScreen().bounds.size.width - authorizationButtonX * 2
let authorizationButtonY = UIScreen.mainScreen().bounds.size.height - 10.0 - authorizationButtonHeight
authorizationButton.frame = CGRectMake(authorizationButtonX, authorizationButtonY, authorizationButtonWidth, authorizationButtonHeight)
authorizationButton.layer.shadowPath = UIBezierPath(rect:CGRectMake(authorizationButton.frame.origin.x, authorizationButton.frame.origin.y,
authorizationButton.frame.size.width, authorizationButton.frame.size.height)).CGPath
authorizationButton.layer.shadowColor = UIColor.blackColor().CGColor
authorizationButton.layer.shadowOpacity = 1.0
authorizationButton.layer.shadowRadius = 3.0
authorizationButton.layer.shadowOffset = CGSizeMake(3.0, 3.0)
我的真实阴影路径要复杂得多
根本不显示阴影
可能是什么问题?问题与贝塞尔路径有关:原点应为0,0 更改代码如下:
authorizationButton.layer.shadowPath = UIBezierPath(rect:
CGRectMake(0,
0,
authorizationButton.frame.size.width,
authorizationButton.frame.size.height)).CGPath
我知道这已经晚了,但对于仍然偶然发现这个问题的人,请将阴影代码移动到一个扩展,并在viewDidLayoutSubviews或viewWillLayoutSubviews中调用它,因为阴影路径需要计算视图的边界或帧 例如:
覆盖func viewDidLayoutSubviews(){super.viewDidLayoutSubviews()myButton.applyShadows()}
是否尝试将clipToBounds设置为false?另外,可能您必须使用origin(0,0)@mxb,doh来实例化贝塞尔路径。。。你说得对,原点应该是(0,0)。把你的评论作为回答,我会接受的。谢谢:)通常情况下,您在此处创建的rect与authorizationButton.bounds
相同。非常感谢CGRect(原点:CGPoint.zero,大小:frame.size)
和bounds
两者都工作正常:)