簡體   English   中英

如何使用圓角向 UIView 添加內陰影

[英]How to add inner shadow to UIView with rounded corners

我必須為具有圓角的UIView添加內陰影。

我在 SO 上看到了幾個答案,它們向 UIViews 添加了內部陰影,但它們不能按我的意願工作,因為它們將陰影添加到整個矩形,而不是考慮圓角。

將此圖像視為我想要實現的示例:

在此處輸入圖像描述

這是一個把戲。 您將陰影和邊框賦予同一視圖,陰影將落在視圖內。 請確保視圖的背景顏色清晰。 使用下面的代碼作為參考。

yourView.layer.shadowColor = UIColor.gray.cgColor
yourView.layer.shadowOpacity = 0.3
yourView.layer.shadowOffset = CGSize.zero
yourView.layer.shadowRadius = 6
yourView.layer.masksToBounds = true
yourView.layer.borderWidth = 1.5
yourView.layer.borderColor = UIColor.white.cgColor
yourView.layer.cornerRadius = imageView.bounds.width / 2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM