目前,Method
内的UIView
已添加为子视图。我想知道如何保留Method的属性,使其框架决定UIView
的大小。
NSObject的:
public class func drawCanvas1(frame frame: CGRect = CGRect(x: 0, y: 0, width: 86, height: 31)) {
let rectanglePath = UIBezierPath(rect: CGRect(x: frame.minX + frame.width - 86, y: frame.minY + floor((frame.height) * 0.00000 - 0.5) + 1, width: 86, height: frame.height - 1 - floor((frame.height) * 0.00000 - 0.5)))
UIColor.grayColor().setFill()
rectanglePath.fill()
}
的UIView:
class shapeTestUI: UIView {
override func drawRect(rect: CGRect) {
StyleKitName.drawCanvas1()
}
}
查看控制器:
var block1: shapeTestUI = shapeTestUI()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(red: 38.0 / 255, green: 151.0 / 255, blue: 68.0 / 255, alpha: 1)
let block = createBlock(block1)
self.view.addSubview(block)
}
func createBlock(blocks:UIView) -> UIView {
let block = blocks as UIView!
//block.frame = CGRectMake(0, 0, 50, 50)
return block
}
答案 0 :(得分:1)
首先,您的代码未显示"方法"被添加为子视图,因为这是不可能的。请阅读:https://en.wikipedia.org/wiki/Method_(computer_programming)。
其次,覆盖drawRect
函数不会添加子视图,而是根据其中的代码(即PaintCode StyleKit函数)绘制UIView
本身。
第三,如果您希望UIView的框架由绘图代码决定,请改变您的PaintCode函数,如下所示:
public class func drawCanvas1(frame: CGRect) {
let rectanglePath = UIBezierPath(rect: CGRect(x: frame.minX + frame.width - 86, y: frame.minY + floor((frame.height) * 0.00000 - 0.5) + 1, width: 86, height: frame.height - 1 - floor((frame.height) * 0.00000 - 0.5)))
UIColor.grayColor().setFill()
rectanglePath.fill()
}
接下来,在shapesTestUI
中,您应该将rect
的参数drawRect
传递给修改后的PaintCode函数:
class shapeTestUI: UIView {
override func drawRect(rect: CGRect) {
StyleKitName.drawCanvas1(rect)
}
}
最后,在视图控制器中初始化block1
时,您应该提供所需的CGRect
var block1: shapeTestUI = shapeTestUI(frame: CGRect(x: 0, y: 0, width: 86, height: 31))