我创建了一个 UIStackView
,其中包含用于启动图库的 UIImageView
。但是,此 UIStackView
将仅显示 3 个项目,第三个项目将覆盖图库中的图像数量。类似于下图:
问题是;如何在 Swift 中的 UIImageView
上添加黑色色调和“+3”文本?
最佳答案
- 像主视图一样创建 UIView“dynamicView”
- 创建一个 UIImageView 添加到“dyanamicView”
- 为 transparentColor shadow.named as "tranpView"创建另一个 View
- 创建标签并将位置居中。标签添加到“tranpView”
最后将“dynamicView” View 添加到 self.view
let dynamicView=UIView(frame: CGRectMake(0, 200, 200, 200)) dynamicView.backgroundColor=UIColor.clearColor() let imageView=UIImageView(frame: CGRectMake(0, 0, 200, 200)) imageView.image = UIImage(named: "bg.png") dynamicView.addSubview(imageView) let tranpView=UIView(frame: CGRectMake(0, 0, 200, 200)) tranpView.backgroundColor = UIColor.whiteColor() tranpView.backgroundColor = tranpView.backgroundColor!.colorWithAlphaComponent(0.3) let label = UILabel(frame: CGRectMake(0, 0, 50, 50)) label.text = "+3" label.font = UIFont.systemFontOfSize(20) label.center = tranpView.center; tranpView.addSubview(label) dynamicView.addSubview(tranpView) self.view.addSubview(dynamicView) //Just add to stackView directly replace the "self.view"
关于ios - 如何在 Swift 中使用色调和一些文本向 UIImageView 添加叠加层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38389856/