我有一个自定义UIView
子类,我想添加作为我的UIViewController
子视图。问题是,即使所有设置都正确(在viewDidLoad
视图具有正确的框架,而不是hidden
),并且它在Interface Builder(picture)中显示,但运行该应用程序时仍未显示视图。在设备上,我只看到一个红色的屏幕,中间没有三角形。iOS - 自定义UIView不显示在设备上
这里的视图子类:
@IBDesignable
class TriangleView: UIView {
override func drawRect(rect: CGRect) {
super.drawRect(rect)
let path = UIBezierPath()
let width = rect.size.width
let height = rect.size.height
let startingPoint = CGPoint(x: center.x, y: center.y - height/2)
path.moveToPoint(startingPoint)
path.addLineToPoint(CGPoint(x: center.x + width/2, y: center.y - height/2))
path.addLineToPoint(CGPoint(x: center.x, y: center.y + height/2))
path.addLineToPoint(CGPoint(x: center.x - width/2, y: center.y - height/2))
path.closePath()
let shapeLayer = CAShapeLayer()
shapeLayer.frame = rect
shapeLayer.position = center
shapeLayer.path = path.CGPath
shapeLayer.fillColor = UIColor.whiteColor().CGColor
layer.mask = shapeLayer
layer.backgroundColor = UIColor.redColor().CGColor
}
}
我没有任何其他的代码显示,我只是认为添加到ViewController
并设置其约束和类TriangleView
。
如果你设置一个断点您的自定义视图的drawRect,将它的调试器停止? – heximal
是的,一切都按原样运作,它只是不显示。忘了提及,在代码中添加视图也不起作用。 – smeshko
您是否尝试过使用Xcode中的Debug View Hierarchy特性来检查运行时的视图? – heximal