ios - 如何在 Swift 中使用色调和一些文本向 UIImageView 添加叠加层?

标签 ios swift uiimageview uiimage

我创建了一个 UIStackView,其中包含用于启动图库的 UIImageView。但是,此 UIStackView 将仅显示 3 个项目,第三个项目将覆盖图库中的图像数量。类似于下图:

enter image description here

问题是;如何在 Swift 中的 UIImageView 上添加黑色色调和“+3”文本?

最佳答案

  1. 像主视图一样创建 UIView“dynamicView”
  2. 创建一个 UIImageView 添加到“dyanamicView”
  3. 为 transparentColor shadow.named as "tranpView"创建另一个 View
  4. 创建标签并将位置居中。标签添加到“tranpView”
  5. 最后将“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"
    

    Output

关于ios - 如何在 Swift 中使用色调和一些文本向 UIImageView 添加叠加层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38389856/

相关文章:

swift - Kingfisher 使用自定义 ImageView 下载多个图像

ios - WatchKit App只启动iPhone模拟器和WatchKit App,不启动IOS App

ios - 为什么在 Swift 中创建默认值数组时不需要 <Element> ?

json - SWIFT 3 如何从本地主机 Web 服务连接 Json

SwiftUI: View 模型内发布的字符串更改不会更新 View

c# - UIImageView 在触摸时更改 UIImage 然后恢复以前的 UIImage

OSX 的 iOS Segue 替代品

ios - 如何将 ImageView 包含到自定义 Segue 中?

swift - 使用 JSON 数组填充 Swift 数组

iPhone开发-UIImageView内存问题