【发布时间】:2017-04-23 14:29:42
【问题描述】:
我已经创建了自定义 UIView
import UIKit
@IBDesignable
class CategoryIcon: UIView {
let pi:CGFloat = CGFloat(M_PI)
@IBInspectable var circleColor:UIColor = UIColor.green
@IBInspectable var width:CGFloat = 10
@IBInspectable var radius: CGFloat = 20
override func draw(_ rect: CGRect) {
center = CGPoint(x:bounds.width/2 ,y:bounds.height/2)
let path = UIBezierPath(ovalIn: rect)
path.lineWidth = width
circleColor.setStroke()
path.stroke()
}
}
但是当我将它添加到故事板时,我把它放在父视图的左上角并不重要as you can see dots shows that view has moved but drawings are out of view and at the left top
问题是什么,如何解决?
【问题讨论】:
-
您是不是想说,无论您尝试将视图放在哪里,它始终处于相同的位置?或者当您尝试更改位置时位置是否会发生变化。
-
@joshLor 无论我移动它,它总是停留在父视图的左上角。但问题是,如果您查看我在表示视图边界的 8 个点中提供的链接表明它已经移动,实际上我绘制的内容不在父视图左上角的 UIView 中
-
你能展示一下你是如何在视图控制器中实现它的吗?
-
2 问题:1. 您是否有代码在您的视图中没有显示。 2. center 是关键字吗?因为我从来没有使用过你实现路径的方法。
-
@joshLor 不,当我尝试用圆弧画圆时,中心来自上一部分。我为 UIView 拥有的所有代码在课外或课外什么都不做。
标签: swift xcode uiview custom-view