我有一个包含其他视图的视图。 ViewforShadow->视图-> bottomBlueView
func setupMyView() {
myView.layer.shadowColor = #colorLiteral(red: 0.2901960784, green: 0.2901960784, blue: 0.2901960784, alpha: 1)
myView.layer.shadowOffset = CGSize.zero
myView.layer.shadowRadius = 4
myView.layer.shadowOpacity = 1
myView.layer.masksToBounds = false
myView.layer.shadowPath = UIBezierPath(roundedRect: myView.bounds, cornerRadius: myView.frame.height / 10).cgPath
myView.layer.backgroundColor = UIColor.clear.cgColor
}
每当我旋转设备时,都会出现这个奇怪的阴影
我知道会发生此问题,因为阴影在纵向/横向模式下具有myView.bounds。因此,每当我旋转设备时都需要更改它。 我发现viewWillLayoutSubviews()和viewDidLayoutSubviews()是解决我的问题的好函数。
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
//update shadow
myView.layer.shadowPath = UIBezierPath(roundedRect: myView.bounds, cornerRadius: myView.frame.height / 10).cgPath
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
//update shadow
myView.layer.shadowPath = UIBezierPath(roundedRect: myView.bounds, cornerRadius: myView.frame.height / 10).cgPath
}
我尝试了两者,并且效果很好,但是每当我旋转设备约1秒钟时,就会出现此毛刺阴影(我不喜欢它)。 1秒后
编辑 我决定创建一个新的空白项目,看看为什么它不起作用。我的View控制器和这段代码中有1个UIView。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var myView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
setupMyView()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
//update shadow
myView.frame = myView.frame
myView.layer.shadowPath = UIBezierPath(roundedRect: myView.bounds, cornerRadius: myView.frame.height / 10).cgPath
}
func setupMyView() {
myView.layer.shadowColor = #colorLiteral(red: 0.2745098174, green: 0.4862745106, blue: 0.1411764771, alpha: 1)
myView.layer.shadowOffset = CGSize.zero
myView.layer.shadowRadius = 4
myView.layer.shadowOpacity = 1
myView.layer.masksToBounds = false
myView.layer.shadowPath = UIBezierPath(roundedRect: myView.bounds, cornerRadius: myView.frame.height / 10).cgPath
// myView.layer.backgroundColor = UIColor.clear.cgColor
}
答案 0 :(得分:0)
您可以尝试通过以下方式设置ViewForShadow:
在ViewController中:
fileprivate func setupMyView() {
viewForShadow.layer.shadowColor = #colorLiteral(red: 0.2901960784, green: 0.2901960784, blue: 0.2901960784, alpha: 1)
viewForShadow.layer.shadowOffset = CGSize.zero
viewForShadow.layer.shadowRadius = 4
viewForShadow.layer.shadowOpacity = 1
viewForShadow.backgroundColor = .clear
}
如您所见,没有shadowPath
。
WhiteView(包含bottomBlueView的视图)将进行一些角半径设置:
fileprivate func setupWhiteView() {
whiteView.layer.cornerRadius = 5.0
whiteView.layer.masksToBounds = true
}
那应该足够了。