简体   繁体   English

Swift UIView Shadow仅适用于iPhone5

[英]Swift uiview shadow only works on iphone5

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. 它可以在Iphone 5上使用,但尝试在iphone 6 plus上使用时,阴影只会应用2/3的视图长度。

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 高度:41

Leading to superview: 0 导致超级观看:0

Trailing to superview: 0 尾随superview:0

You're applying your shadow before layout has completed as @Tommy mentioned in the comments. 您要在布局完成之前应用阴影,如评论中提到的@Tommy。

Move your code from your viewDidLoad to your viewDidLayoutSubviews . 将代码从viewDidLoad移到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? 另外,您是否在视图上设置了Y约束? I'd imagine you'd want to pin it to the top of your Superview . 我想像你会想pin到您的顶部Superview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM