gpt4 book ai didi

ios - 如何在 iOS 中创建带有阴影和 subview 的 UIimageView

转载 作者:行者123 更新时间:2023-11-29 11:35:34 25 4
gpt4 key购买 nike

如何使用 Objective-C 创建具有阴影效果和 subview (小图像图标和标签)的 UIImageView。图像可能如下所示:

White image with shadow and small image and label as subview on top of it

 self.view.backgroundColor = [UIColor whiteColor];

UIImageView* image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"profImg.jpeg"]];
image.frame = CGRectMake(40, 200, 150, 100);
image.layer.cornerRadius = 9;
image.layer.shouldRasterize = YES;
image.layer.masksToBounds = YES;

// make new layer to contain shadow and masked image
CALayer* containerLayer = [CALayer layer];
containerLayer.shadowColor = [UIColor blackColor].CGColor;
containerLayer.shadowRadius = 9.f;
containerLayer.shadowOffset = CGSizeMake(1.f, 2.f);
containerLayer.shadowOpacity = .6f;

// add masked image layer into container layer so that it's shadowed
[containerLayer addSublayer:image.layer];

// add container including masked image and shadow into view
[self.view.layer addSublayer:containerLayer];

我无法在该图像上添加子图像和标签。

最佳答案

UIImageView 边框:

imageView.layer.borderWidth = 5  
imageView.layer.borderColor = UIColor.gray.cgColor // Set as per requirement

subview :

其实没有必要把label(Title)作为imageView的子view。保持和imageView同级。

let labelTitle = UILabel(frame: CGRect(x: imageView.frame.origin.x, y: (imageView.frame.origin.y + imageView.frame.size.height - 30), width: imageView.frame.size.width, height: 20))  
labelTitle.text = "Title"
labelTitle.backgroundColor = UIColor.gray // Keep it same as imageView's border color
self.view.addSubView(labelTitle) // replace self.view by imageView's parent
self.view.bringSubview(toFront: labelTitle)

关于ios - 如何在 iOS 中创建带有阴影和 subview 的 UIimageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49575217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com