【问题标题】:How to add shadow only to top and bottom to UIVIEW in swift如何在swift中仅向UIVIEW的顶部和底部添加阴影
【发布时间】:2021-05-04 06:41:10
【问题描述】:

我是 swift 新手,我正在尝试向 UIView 添加阴影。 我的代码是这样的

ViewPay.layer.masksToBounds = false
ViewPay.layer.shadowRadius = 2
ViewPay.layer.shadowOpacity = 1
ViewPay.layer.shadowColor = UIColor.red.cgColor
ViewPay.layer.shadowOffset = CGSize(width: 0 , height:2) 

但它给所有侧面都添加了阴影

如何添加这样的阴影

【问题讨论】:

  • 您的白色视图是否在另一个父母的视图中?如果是,则制作一个剪辑以将 true 绑定到父视图并设置 shdowOffset = .zero。
  • 你展示的图片是预期的结果还是你的代码的输出?
  • 或使用贝塞尔路径:stackoverflow.com/a/47010380/14733292
  • 期待您使用 2 视图来实现它。

标签: ios swift


【解决方案1】:

一种解决方案是将您的ViewPay 放入容器 UIView 中。

将您的ViewPay 设置为容器视图的前缘和后缘,并为顶部和底部添加一些填充以显示阴影。

还将容器视图clipsToBounds 设置为true。

【讨论】:

    【解决方案2】:

    我希望能帮助您解决问题。如果您想要一些简单易懂的东西,并遵循提示。您创建一个大容器并添加所需的视图。紫色的垂直视图,另一个灰色的水平视图,中间放置一个图像,就像我在代码中所做的那样:

    
    import UIKit
    
    class ViewController: UIViewController {
      
        
         override func viewDidLoad() {
            super.viewDidLoad()
             
            //Big container
            let container = UIView(frame: CGRect(x: self.view.bounds.width * 0.25, y: self.view.bounds.height * 0.5, width: 250, height: 290))
            container.backgroundColor = .clear
            self.view.addSubview(container)
            
            //Purple view
            let viewContainer = UIView(frame: CGRect(x: container.bounds.midX , y: 0, width: container.bounds.width * 0.89, height: container.bounds.height))
    
            viewContainer.layer.anchorPoint.x = 1.0
    
            viewContainer.backgroundColor = UIColor(displayP3Red: 158/255, green: 131/255, blue: 178/255, alpha: 1.0)
            viewContainer.layer.cornerRadius = 8
            viewContainer.clipsToBounds = true
    
            container.addSubview(viewContainer)
    
            //Gray view
            let viewContainer2 = UIView(frame: CGRect(x: container.bounds.midX , y: container.bounds.midY, width: container.bounds.width * 0.93, height: container.bounds.height * 0.86))
    
            viewContainer2.layer.anchorPoint.y = 1.0
            viewContainer2.layer.anchorPoint.x = 1.0
    
    
            viewContainer2.backgroundColor = UIColor(white: 0.5, alpha: 0.3)
            viewContainer2.layer.cornerRadius = 5
            viewContainer2.clipsToBounds = true
    
            container.addSubview(viewContainer2)
            
            //image
            let imageView = UIImageView(frame: CGRect(x: container.bounds.midX, y: container.bounds.midY, width: container.bounds.width * 0.90, height: container.bounds.height * 0.90))
            imageView.contentMode = .scaleToFill
            imageView.layer.anchorPoint = CGPoint(x: 1.0, y: 1.0)
            imageView.layer.cornerRadius = 10
            imageView.clipsToBounds = true
            imageView.image = UIImage(named: "image name")
            container.addSubview(imageView)
    
         }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-30
      • 2016-10-06
      • 1970-01-01
      • 1970-01-01
      • 2017-12-01
      • 1970-01-01
      • 2011-10-04
      • 2017-08-23
      相关资源
      最近更新 更多