UIView边界/框架与drawRect不匹配

问题描述:

我正在学习如何创建自定义UIViews的过程。我正在制作的这个特定视图包含一对夫妇按钮。我注意到当我从lazy实例化块中调用frame/height属性时,我得到的值是128,但是当我在drawRect函数中传递的rect参数上调用height属性以及边界时,我得到值94。UIView边界/框架与drawRect不匹配

我不明白为什么从drawRect函数调用的边界/框架宽度/高度属性与初始值设定项中使用的框架/边界高度和宽度不匹配。有人可以解释这个吗?

@IBDesignable 
class GroupingMetaDataView: UIView { 

    @IBInspectable var strokeColor: UIColor = 
     UIColor.blackColor().colorWithAlphaComponent(0.4) 

    @IBInspectable var strokeWidth: CGFloat = 2.0 


    lazy var subButton: AddButton! = { 
     print("frame height: \(self.frame.height)") 
     print("bounds height: \(self.bounds.height)") 
     let width = self.frame.width * 0.087 
     let size = CGSize(width: width, height: width) 
     let frame = CGRect(origin: CGPoint(x: self.frame.width * 0.055, 
              y: self.frame.height * 0.5), size: size) 
     let button = AddButton(frame: frame, isAddButton: false) 

     return button 
    }() 

    lazy var addButton: AddButton! = { 
     let width = self.frame.width * 0.087 
     let size = CGSize(width: width, height: width) 
     let frame = CGRect(origin: CGPoint(x: self.frame.width * 0.857, 
              y: self.frame.height), size: size) 
     let button = AddButton(frame: frame, isAddButton: true) 

     return button 
    }() 

    override init(frame: CGRect) { 
     super.init(frame: frame) 

     self.setupUI() 

    } 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     self.setupUI() 
    } 

    // Only override drawRect: if you perform custom drawing. 
    // An empty implementation adversely affects performance during animation. 
    override func drawRect(rect: CGRect) { 
     // Drawing code 
     print("rect height: \(rect.height)") 
     print("boundsheight: \(bounds.height)") 

     let context = UIGraphicsGetCurrentContext() 
     CGContextSetLineWidth(context, strokeWidth) 
     strokeColor.setStroke() 

     let topBarPath = UIBezierPath() 
     topBarPath.moveToPoint(CGPoint(x: 0.0, y: 2.0)) 
     topBarPath.addLineToPoint(CGPoint(x: rect.width, y: 2.0)) 

     topBarPath.lineWidth = strokeWidth 
     topBarPath.stroke() 

     let bottomBarPath = UIBezierPath() 
     bottomBarPath.moveToPoint(CGPoint(x: 0.0, y: rect.height-2.0)) 
     bottomBarPath.addLineToPoint(CGPoint(x: rect.width, y: rect.height-2.0)) 

     bottomBarPath.lineWidth = strokeWidth 
     bottomBarPath.stroke() 
    } 

    // MARK: Setup 

    func setupUI() { 
     self.backgroundColor = UIColor.yellowColor() 
     addSubview(subButton) 
     addSubview(addButton) 
    } 
} 

编辑:

我创建从视图控制器的GroupingMetaDataView。 YardageMetaDataView是GroupingMetaDataView的一个子类。 YardageMetaDataView不执行任何自定义绘图。

我注意到了的drawRect参数的文档指出

需要的视图的边界的部分进行更新。第一个 时间您的视图绘制,这个矩形通常是整个 视图的可见边界。但是,在随后的绘制 操作中,矩形可能只指定部分视图。

何时以及为什么参数rect仅指定视图的一部分?在触发setNeedsDisplay之后,drawRect在下一个绘制周期被调用。 SetNeedsDisplay不接受任何参数,所以我假设参数代表整个视图?

ViewController.swift

class ViewController: UIViewController { 

// MARK: - Properties 

    lazy var yMetaDataView: YardageMetaDataView! = { 
     let view = YardageMetaDataView(frame: CGRect(origin: CGPoint(x: 50, y: 100), 
            size: CGSize(width: self.view.bounds.width * 0.75, 
               height: self.view.bounds.height * 0.102))) 
     view.translatesAutoresizingMaskIntoConstraints = false 
     return view 
    }() 
} 
+0

代码在哪里添加到父视图? – nhgrif

+0

@nhgrif我刚刚添加了实例化视图的代码。 – gofly

有类似setNeedsDisplay称为setNeedsDisplay(_ :)另一种方法。当后者被调用时,参数传递给drawRect(_ :)是您在该调用中传递的参数。该矩形区域被标记为无效并需要重绘。

我不知道你是否叫setNeedsDisplay(_ :)与否。找出答案是你的工作。祝你好运。 :)