ios - 这里的角落工作但阴影在 iOS 9 和 10 中不起作用

标签 ios swift autolayout shadow cornerradius

Here corner is working but shadow is not work in iOS 9 and 10

and iOS 11 both are working fine

 viewBlueMe.chatCellDesign([.topLeft,.bottomLeft,.topRight], [.layerMinXMinYCorner,.layerMinXMaxYCorner,.layerMaxXMinYCorner], radius: 5)

扩展名:

    extension UIView {
    func chatCellDesign(_ corners:UIRectCorner,_ cormerMask:CACornerMask, radius: CGFloat) {

        if #available(iOS 11.0, *){
            self.clipsToBounds = false
            self.layer.cornerRadius = radius
            self.layer.maskedCorners = cormerMask

            self.layer.shadowColor = UIColor.gray.cgColor
            self.layer.shadowOpacity = 1
            self.layer.shadowOffset = CGSize.zero
            self.layer.shadowRadius = 5
        }else{
            let rectShape = CAShapeLayer()
            rectShape.bounds = self.frame
            rectShape.position = self.center
            rectShape.path = UIBezierPath(roundedRect: self.bounds,    byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius)).cgPath
            self.layer.mask = rectShape

            // FIXME: THIS CODE IS NOT WORK
            self.layer.shadowColor = UIColor.gray.cgColor
            self.layer.shadowOpacity = 1
            self.layer.shadowOffset = CGSize.zero
            self.layer.shadowRadius = 5


        }
    }
}

我在 iOS 9 和 10 中需要这种类型的输出,这也是 iOS11

提前致谢 enter image description here

最佳答案

创建一个 outerview 并将您的 UILabel 放入其中,请查看以下详细信息,

outerView.layer.cornerRadius = 5.0
outerView.layer.shadowColor = UIColor.black.cgColor
outerView.layer.shadowOffset = CGSize.zero
outerView.layer.shadowOpacity = 0.2
outerView.layer.shadowRadius = 5.0

更新

见下图,

enter image description here

这是IBOutlets

@IBOutlet weak var tmpView: UIView!
@IBOutlet weak var innerView: UIView!

你可以这样做,

innerView.roundCorners([.topLeft, .topRight, .bottomLeft], radius: 5)
tmpView.layer.shadowColor = UIColor.black.cgColor
tmpView.layer.shadowOffset = CGSize.zero
tmpView.layer.shadowOpacity = 0.2
tmpView.layer.shadowRadius = 10.0

在这里找到 UIView 扩展,

extension UIView {
    func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
    }
}

这会给你下面的输出,

enter image description here

仅供引用。尝试使用 shadow 属性以获得您想要的精确输出。

关于ios - 这里的角落工作但阴影在 iOS 9 和 10 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50346684/

相关文章:

ios - 在 Swift 中滚动到 UITextView 的特定部分

ios - 界面生成器 : How to clean up the deleted constraint?

ios - 在哪里更改自动布局约束的 "installed"?

iOS 大小类 : Restrict to Single ViewController

ios - 如何在 Objective C 中使径向渐变在 View 中间淡出

IOS 9 native 打印预览不呈现

ios - if 语句中的 switch 语句

ios - 如何在 Xcode 8 中重新排序 UITabBarController 中的关系序列

ios - 将值返回给 tableView 函数

ios - 字典 [字符串 : String] keys order changed when converted to array swift