暗影在iphone上是完美的,但在ipad上它变得非常短暂

时间:2018-02-19 12:26:36

标签: ios swift uitableview uiview

enter image description here

这是iphone的屏幕截图,这里阴影非常好但是当我在ipad上运行应用程序时就变成了这样......

enter image description here

现在这个问题我想让它像ipad上的第一张图片一样..

我正在使用此代码删除阴影

extension UIView {

    // OUTPUT 1
    func dropShadow(scale: Bool = true) {
        layer.masksToBounds = false
        layer.shadowColor = UIColor.black.cgColor
        layer.shadowOpacity = 0.5
        layer.shadowOffset = CGSize(width: -1, height: 1)
        layer.shadowRadius = 1
        layer.shadowPath = UIBezierPath(rect: bounds).cgPath
        layer.shouldRasterize = true
        layer.rasterizationScale = scale ? UIScreen.main.scale : 1
    }

    // OUTPUT 2
    func dropShadow(color: UIColor, opacity: Float = 0.5, offSet: CGSize, radius: CGFloat = 1, scale: Bool = true) {
        layer.masksToBounds = false
        layer.shadowColor = color.cgColor
        layer.shadowOpacity = opacity
        layer.shadowOffset = offSet
        layer.shadowRadius = radius
        layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath
        layer.shouldRasterize = true
        layer.rasterizationScale = scale ? UIScreen.main.scale : 1
    }
}

使用此行在任何视图上删除阴影

shadowView.dropShadow(color: .black, opacity: 0.7, offSet: CGSize(width: -1, height: 1), radius: 5, scale: true)

任何人都知道问题出在哪里。??

提前感谢:)

1 个答案:

答案 0 :(得分:1)

问题是当视图布局不正确时你正在使用bounds值。

在viewDidLayoutSubviews中设置阴影。别忘了打电话给super.didlayoutsubviews()

或者你可以致电

view.layoutIfNeeded()