【问题标题】:How can we set a UIView as a mask of another UIView我们如何将一个 UIView 设置为另一个 UIView 的掩码
【发布时间】:2018-01-16 11:19:50
【问题描述】:

我有

  1. gradientView 只是我的渐变视图
  2. 带有 anImageView 和 aUILabel 的视图 containerView

现在当我尝试

gradientView.mask = containerView

什么都没有显示???但是当我这样做时

gradientView.mask = anImageView

gradientView.mask = aUILabel

它很好地涂抹面膜。 有什么办法可以实现

gradientView.mask = containerView

【问题讨论】:

    标签: ios swift uiview swift4 cagradientlayer


    【解决方案1】:

    UIImageViewUILabel 都具有固有内容大小,但没有 UIView。视图的掩码必须正确设置框架。确保设置了containerView.frame

    containerView.frame = gradientView.bounds
    

    另外,请确保 containerView.backgroundColor 设置为 UIColor.clear

    【讨论】:

    • 我只想添加另一件事,视图的背景颜色应该是 clearColor
    • @AsadullahAli 所以它不起作用?或者你的意思是视图的背景颜色应该是clearColor?
    • 说对了一半。我必须添加 containerView.backgroundColor = UIColor.clear
    • @AsadullahAli 哦,好的,我会更新答案。我预计containerView 的背景颜色是clearColor,因为这是UIView 的默认背景颜色
    • 当我们从情节提要中添加它时,默认颜色是白色,这导致我浪费了数小时的时间,所以最好让其他人知道。
    猜你喜欢
    • 2017-09-25
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 2019-04-14
    • 2011-10-01
    • 1970-01-01
    相关资源
    最近更新 更多