首页 > 解决方案 > UINavigationController 堆栈中的阴影

问题描述

UINavigationController堆栈中,如何更改最顶层 viewController 的图层属性,例如.view.layer.shadowRadius

换句话说,我需要更改默认的 shadowRadius,它在滑回RootViewController.

非常感谢。

编辑:问题不在于创建属性,而是在堆栈中找到正确的视图/接受它作为替代的 shadowRadius。

标签: iosswiftuinavigationcontroller

解决方案


最好的方法是在你的类中添加这个扩展,在你的类的末尾添加扩展,并将阴影和角半径添加到任何视图以及导航控制器:)

extension UIView {


@IBInspectable
var cornerRadius: CGFloat {
    get {
        return layer.cornerRadius
    }
    set {
        layer.cornerRadius = newValue
    }
}

@IBInspectable
var borderWidth: CGFloat {
    get {
        return layer.borderWidth
    }
    set {
        layer.borderWidth = newValue
    }
}

@IBInspectable
var borderColor: UIColor? {
    get {
        if let color = layer.borderColor {
            return UIColor(cgColor: color)
        }
        return nil
    }
    set {
        if let color = newValue {
            layer.borderColor = color.cgColor
        } else {
            layer.borderColor = nil
        }
    }
}

@IBInspectable
var shadowRadius: CGFloat {
    get {
        return layer.shadowRadius
    }
    set {
        layer.shadowRadius = newValue
    }
}

@IBInspectable
var shadowOpacity: Float {
    get {
        return layer.shadowOpacity
    }
    set {
        layer.shadowOpacity = newValue
    }
}

@IBInspectable
var shadowOffset: CGSize {
    get {
        return layer.shadowOffset
    }
    set {
        layer.shadowOffset = newValue
    }
}

@IBInspectable
var shadowColor: UIColor? {
    get {
        if let color = layer.shadowColor {
            return UIColor(cgColor: color)
        }
        return nil
    }
    set {
        if let color = newValue {
            layer.shadowColor = color.cgColor
        } else {
            layer.shadowColor = nil
        }
    }
}
}

推荐阅读