【问题标题】:Frame a UIView with transparent region用透明区域框住 UIView
【发布时间】:2017-02-09 00:47:37
【问题描述】:

我在我的 UIViewController 中的 IB 中放置了一个普通的 UIView,并使用以下绘制方法将视图设置为 UIView 子类:

override func draw(_ rect: CGRect) {
    super.draw(rect)

    self.isOpaque = false
    self.backgroundColor?.setFill()
    UIRectFill(rect)
    let insetRect = self.bounds.insetBy(dx: 0, dy: 4)
    UIColor.clear.setFill()
    UIRectFill(insetRect)
}

预期的结果应该是背景颜色(白色)在视图的顶部和底部显示为一条线。这确实是发生的事情。但我希望插入矩形是透明的,这就是我将颜色设置为清除的原因。但结果是黑色的。看图。

【问题讨论】:

    标签: uiview swift3


    【解决方案1】:

    我找到了另一种方法,子类化 UIView,这一次我在中间得到了预期的透明度:

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.clear
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    
    
    override func draw(_ rect: CGRect) {
        super.draw(rect)
    
        let top = UIBezierPath(rect: CGRect(x: 0, y: 0, width: self.bounds.width, height: 4))
        UIColor.white.setFill()
        top.fill()
        let bottom = UIBezierPath(rect: CGRect(x: 0, y: self.bounds.height - 4, width: self.bounds.width, height: 4))
        bottom.fill()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-20
      • 2013-04-28
      • 1970-01-01
      • 1970-01-01
      • 2011-07-16
      • 2011-08-20
      相关资源
      最近更新 更多