将阴影添加到UIView的顶部

时间:2014-06-09 20:22:53

标签: ios objective-c uiview

如何在我UIView的顶部添加阴影我尝试过以下但没有运气......

childView.layer.cornerRadius = 5;
childView.layer.masksToBounds = YES;
childView.layer.shadowOffset = CGSizeMake(-15, 20);
childView.layer.shadowRadius = 5;
childView.layer.shadowOpacity = 0.5;

6 个答案:

答案 0 :(得分:13)

Swift 3扩展:

这包括我正在处理的应用的默认值,但您可以更改它们以匹配您在应用中所需的样式。

if __name__ == '__main__':
    app = QApplication(sys.argv)
    web = QWebEngineView()
    web.settings().setAttribute(QWebEngineSettings.FullScreenSupportEnabled, True)
    web.page().fullScreenRequested.connect(lambda request: request.accept())
    baseUrl = "local"
    htmlString = """
            <iframe width="560" height="315" src="https://www.youtube.com/embed/L0MK7qz13bU?rel=0&amp;showinfo=0" frameborder="0" allowfullscreen></iframe>
             """
    web.setHtml(htmlString, QUrl(baseUrl))

    web.show()
    sys.exit(app.exec_())

答案 1 :(得分:5)

您需要将子视图的masksToBounds属性设置为NO才能使阴影可见。

childView.layer.masksToBounds = NO;

答案 2 :(得分:1)

设置masksToBounds = NO。你没有看到阴影的原因是因为当masksToBounds为YES时,它完全隐藏在你的视图后面。

如果您的按钮已四舍五入,则可以调整视图的imageEdgeInset值。即:UIEdgeInsetsMake(5, 0, 0, 10);

答案 3 :(得分:1)

1。步骤:首先创建uiview出口

2.Step:

    buttonBackgroundView.layer.shadowOpacity = 0.7
    buttonBackgroundView.layer.shadowOffset = CGSize(width: 3, height: 3)
    buttonBackgroundView.layer.shadowRadius = 15.0
    buttonBackgroundView.layer.shadowColor = UIColor.black.cgColor

答案 4 :(得分:0)

尝试将masksToBounds设置为NO。根据此链接Whats the best way to add a drop shadow to my UIView,YES会剪切超出视图边界的图层。

答案 5 :(得分:0)

答案在UIBezierPath

您可以在那里找到一篇非常有趣的文章: https://www.hackingwithswift.com/articles/155/advanced-uiview-shadow-effects-using-shadowpath

layer.shadowOffset = CGSize(width: 0, height: -2)
layer.shadowRadius = 5
layer.shadowPath = UIBezierPath(rect: CGRect(x: -1,
                                             y: -1,
                                             width: self.bounds.width,
                                             height: self.layer.shadowRadius)).cgPath