【问题标题】:ContainerView with shadow and rounded edges带有阴影和圆角边缘的 ContainerView
【发布时间】:2017-03-15 00:35:43
【问题描述】:

我想创建带有阴影和圆角边缘的自定义 ContainerView。这个 ContainerView 是放置在另一个 UIView 顶部的小矩形形式。在这种特殊情况下,额外的图层和使用 CoreGraphics 绘制阴影都没有帮助。

【问题讨论】:

  • 为什么你认为额外的视图/层没有帮助?你的问题是应用cornerRadiuscontainerView 后看不到影子吗?
  • 每次我应用额外的视图/图层时,阴影都是可见的,边缘是圆的,但我无法摆脱这个“白色”区域。
  • 你应该发布你的代码以获得帮助。
  • 你试过设置containerView.layer.masksToBounds = true吗?

标签: ios swift uiview uicontainerview dropshadow


【解决方案1】:

我找到了合适的解决方案。我给ContainerView 投下了阴影,这是每个UIView 内部的超类。然后,我使用UIViewController 类为这个小矩形区域圆角。

class GraphViewController: UIViewController {
    @IBOutlet var graphView: GraphViewRenderer!

    override func viewDidLoad() {
        graphView.layer.cornerRadius = 20.0
        graphView.layer.masksToBounds = true

        super.viewDidLoad()
    }
}

class GraphContainerView: UIView {
    func applyPlainShadow() {
        layer.shadowColor = UIColor.black.cgColor
        layer.shadowOffset = CGSize.zero
        layer.shadowRadius = 5.0
        layer.shadowOpacity = 0.7
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        applyPlainShadow()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        applyPlainShadow()
    }
}

【讨论】:

  • 不是超类,是超视图。那不一样。你的回答基本上就是我的建议。
  • 感谢@alexburtnik 的帮助。
【解决方案2】:

你错了,额外的视图/图层无济于事。

您可以将带有圆角的roundedContainer 放置到另一个shadowedView 中,并在其图层中添加阴影。 为避免出现这些白角,请确保在某处将背景颜色设置为 clear

例子:

//superview for container with rounded corners
shadowedView.backgroundColor = UIColor.clear //this will fix your white corners issue
shadowedView.layer.shadowColor = UIColor.black.cgColor
shadowedView.layer.shadowOffset = .zero
shadowedView.layer.shadowOpacity = 0.3
shadowedView.layer.shadowRadius = 5.0

//add a container with rounded corners
let roundedView = UIView()
roundedView.frame = baseView.bounds
roundedView.layer.cornerRadius = 10
roundedView.layer.masksToBounds = true
shadowedView.addSubview(roundedView)

【讨论】:

    猜你喜欢
    • 2013-02-20
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    相关资源
    最近更新 更多