Uiview Can';在将图像视图拉伸到其边界的图层上设置拐角半径和阴影?

Uiview Can';在将图像视图拉伸到其边界的图层上设置拐角半径和阴影?,uiview,core-animation,uiimageview,calayer,roundedcorners-dropshadow,Uiview,Core Animation,Uiimageview,Calayer,Roundedcorners Dropshadow,这让我很难受。我有一个UIView(称之为“家长”)。该视图最底层的子视图是UIImageView(称之为“子视图”),其框架占据整个“父”边界 我想在“父”视图上圆角,并设置阴影。我像往常一样在“家长”的CALayer上这样做: [[parent layer] setShadowOffset:CGSizeMake(5, 5)]; [[parent layer] setShadowRadius:6]; [[parent layer] setShadowOpacity:0.4]; [[pa

这让我很难受。我有一个UIView(称之为“家长”)。该视图最底层的子视图是UIImageView(称之为“子视图”),其框架占据整个“父”边界

我想在“父”视图上圆角,并设置阴影。我像往常一样在“家长”的
CALayer
上这样做:

[[parent layer] setShadowOffset:CGSizeMake(5, 5)];
[[parent layer] setShadowRadius:6];
[[parent layer] setShadowOpacity:0.4];    
[[parent layer] setCornerRadius:6];
这将正确显示阴影,但不会使拐角变圆

下面是踢球者:

  • 如果我移除“子”图像视图,或者缩小它,使其不占据“父”视图的整个边界,我将在父视图上获得正确的圆角和阴影
  • 如果我不使用“child”,而是在“parent”视图上设置“clipstobunds”,则可以正确地获取角点。但现在阴影消失了
  • 在子对象层上设置拐角半径似乎也没有效果
  • “子”图像视图似乎只是遮挡了“父”视图上的圆角,因为它占据了整个矩形,基于父视图的剪裁可以获得角点,但也遮掩了阴影。不知道为什么#3不起作用

    我错过了什么?我盯着这个看太久了,是否忽略了一些显而易见的事情

    谢谢


    (令人震惊的是,标签“roundedcorners dropshadow”已经存在。太棒了。)

    通常,您必须将剪贴簿设置为具有圆角,但由于您想要保留阴影,因此也必须使阴影的角变圆。您是否尝试过使用贝塞尔路径设置阴影路径?将clipsToBounds/masksToBounds保持为默认值,否。类似于:

      [[parent layer] setCornerRadius:6.0f];
      [[parent layer] setShadowPath:
                 [[UIBezierPath bezierPathWithRoundedRect:[parent bounds] 
                       cornerRadius:6.0f] CGPath]];
    

    您是否尝试过设置子UIImageView的边界,使其也具有圆角?也许这样它就不会覆盖容器视图的阴影。只是一个想法,不确定它是否会起作用。

    您需要两个嵌套视图,内部视图设置圆角并剪裁为绑定,外部视图有阴影(因此不剪裁)。在您的情况下,内部和外部视图可能是“子视图”和“父视图”,但我猜您没有为这些视图设置正确的剪裁值


    请参见中的答案。

    使用Swift 3,您可以从以下两个代码段中选择一个,以便在图像视图或包含图像层的视图上设置拐角半径和阴影


    #1.使用
    UIView
    CALayer
    和弹簧和支柱
    #2.使用
    UIView
    UIImageView
    CALayer
    和自动布局
    两个代码段都会生成以下显示:



    您可以找到更多的方法来将圆角图像和阴影结合在一起。

    如果您希望imageview的阴影层具有圆角半径,则更好的解决方案是将imageview作为具有1点边距的子视图。。及

     imgBrandLogo.backgroundColor = UIColor.blue
        imgBrandLogo.layer.cornerRadius = imgBrandLogo.frame.height/2
        imgBrandLogo.clipsToBounds = true
        viewBrandLogo.layer.shadowColor = UIColor(rgb:0x262626,alpha:0.24).cgColor
        viewBrandLogo.layer.shadowOffset = CGSize(width: 0, height: 1)
        viewBrandLogo.layer.shadowOpacity = 1
        viewBrandLogo.layer.shadowPath = UIBezierPath(roundedRect:imgBrandLogo.bounds , cornerRadius: imgBrandLogo.frame.height/2).cgPath
        viewBrandLogo.backgroundColor = UIColor.clear.withAlphaComponent(0.0)
    

    谢谢你,马特。是的——阴影角的圆角不是问题——无论我使用路径还是常规属性设置阴影角,阴影角都可以工作。设置剪贴簿时根本无法显示阴影,如果不设置,图像视图会消除圆角。(请注意,如果我根本没有“子”图像视图,这两个视图都可以正常工作)如果您不使用图像视图,而是将图层内容设置为所需的图像,会发生什么情况?e、 g.[图层设置内容:(id)[图像CGImage]];您可以设置ContentsGravity:kCAGravityResizeAspectFill以获取图像,以使用aspect fill填充图层,或者如果效果不理想,可以尝试其他常量。在我的情况下(非常类似),我有一个XIB文件,该文件在堆栈底部有一个UIImageView,它具有所显示项目的背景。图像视图正在覆盖其父视图的角半径。不知道如何做你建议Matt的内容设置,因为它是一个xib文件。(不是OP而是同一个问题)@Kalle。我不确定你到底在问什么。你能把它作为一个新的问题,引用这个问题,然后更深入地解释什么不适合你?也许包括一些代码?谢谢。我们终于有赢家了。:)谢谢朱利安。
    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // constants
            let radius: CGFloat = 20, dimension: CGFloat = 200, offset = 8
    
            // image view
            let imageView = UIImageView(image: UIImage(named: "image"))
            imageView.contentMode = .scaleAspectFill
            imageView.layer.cornerRadius = radius
            imageView.layer.masksToBounds = true
    
            // rounded view
            let roundedView = UIView()
            roundedView.layer.shadowColor = UIColor.darkGray.cgColor
            roundedView.layer.shadowPath = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: dimension, height: dimension), cornerRadius: radius).cgPath
            roundedView.layer.shadowOffset = CGSize(width: offset, height: offset)
            roundedView.layer.shadowOpacity = 0.8
            roundedView.layer.shadowRadius = 2
    
            // views hierarchy
            roundedView.addSubview(imageView)
            view.addSubview(roundedView)
    
            // layout
            imageView.translatesAutoresizingMaskIntoConstraints = false
            roundedView.translatesAutoresizingMaskIntoConstraints = false
            roundedView.widthAnchor.constraint(equalToConstant: dimension).isActive = true
            roundedView.heightAnchor.constraint(equalToConstant: dimension).isActive = true
            imageView.widthAnchor.constraint(equalTo: roundedView.widthAnchor).isActive = true
            imageView.heightAnchor.constraint(equalTo: roundedView.heightAnchor).isActive = true
            roundedView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
            roundedView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
            imageView.centerXAnchor.constraint(equalTo: roundedView.centerXAnchor).isActive = true
            imageView.centerYAnchor.constraint(equalTo: roundedView.centerYAnchor).isActive = true
        }
    
    }
    
     imgBrandLogo.backgroundColor = UIColor.blue
        imgBrandLogo.layer.cornerRadius = imgBrandLogo.frame.height/2
        imgBrandLogo.clipsToBounds = true
        viewBrandLogo.layer.shadowColor = UIColor(rgb:0x262626,alpha:0.24).cgColor
        viewBrandLogo.layer.shadowOffset = CGSize(width: 0, height: 1)
        viewBrandLogo.layer.shadowOpacity = 1
        viewBrandLogo.layer.shadowPath = UIBezierPath(roundedRect:imgBrandLogo.bounds , cornerRadius: imgBrandLogo.frame.height/2).cgPath
        viewBrandLogo.backgroundColor = UIColor.clear.withAlphaComponent(0.0)