I am trying to add a shadow under my uivew. It works on a Iphone 5 but the shadow only gets applied 2/3 of the views lenght when trying on a iphone 6 plus.
code:
myView.layer.shadowColor = UIColor.blackColor().CGColor
myView.layer.shadowOpacity = 1
myView.layer.shadowRadius = 5
myView.layer.shadowPath = UIBezierPath(rect: CGRect(x: 0, y: 0, width: myView.frame.width, height: myView.frame.height)).CGPath
The constraints of the view is:
Height: 41
Leading to superview: 0
Trailing to superview: 0
You're applying your shadow before layout has completed as @Tommy mentioned in the comments.
Move your code from your viewDidLoad
to your viewDidLayoutSubviews
.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var myView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidLayoutSubviews() {
myView.layer.shadowColor = UIColor.blackColor().CGColor
myView.layer.shadowOpacity = 1
myView.layer.shadowRadius = 5
myView.layer.shadowPath = UIBezierPath(rect: CGRect(x: 0, y: 0, width: myView.frame.width, height: myView.frame.height)).CGPath
}
Also, have you set a Y
constraint on your view? I'd imagine you'd want to pin
it to the top of your Superview
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.