【问题标题】:Gradient not covering full frame SWIFT渐变不覆盖全画幅 SWIFT
【发布时间】:2018-03-26 23:52:09
【问题描述】:

我有imageView的以下扩展名:

extension UIImageView {
func addBlackGradientLayer(frame: CGRect){
    let gradient = CAGradientLayer()
    gradient.frame = frame
    gradient.colors = [UIColor.clear.cgColor, UIColor.black.cgColor]
    gradient.locations = [0.0, 1.0]
    self.layer.addSublayer(gradient)
}}

imageView.addBlackGradientLayer(frame: imageView.frame)

在将其应用于 imageView 时,我得到以下输出。我尝试设置约束以及自动布局。

以下是执行后的输出:

【问题讨论】:

  • 留下一个拒绝投票的理由是有道理的,否则这是一个愚蠢的举动

标签: ios swift3 uiimageview


【解决方案1】:

你需要打电话

imageView.addBlackGradientLayer(frame: imageView.bounds)

见:Cocoa: What's the difference between the frame and the bounds?

【讨论】:

  • 嘿,我已经试过了。好吧,如果我在 iphone 模拟器 7+ 上运行它,它会显示这个问题。当我在 6s 和 7+ 设备上运行渐变时,渐变是正确可见的。可能是模拟器问题?
  • 我无法在 7+ 模拟器上重现您的错误。
  • 也许你的视图在应用渐变层后会被调整大小。我建议在添加图层时和视图显示后检查框架。
  • 是的,它打印相同的帧大小。问题只是框架。我想知道为什么它会这样,即使设置了适当的约束
  • 我正在使用一个集合标题视图并且有一个完全包裹标题边界的图像视图。在打印框架和绑定值时,它返回相同的值。我真的不知道我做错了什么
猜你喜欢
  • 2014-03-13
  • 1970-01-01
  • 2019-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-27
相关资源
最近更新 更多