我将 NSView 子类化以使其可扩展。以下苹果 ' View Geometry ' 指南,边界应拉伸(stretch)以适合框架。 设置框架大小时,没有任何变化。当我将帧大小打印到控制台时,我看到 setFrameSize 几乎什么也没做。
所以这是我的问题:如何更改设置的帧大小(不设置设置的边界大小)? (禁用自动布局)
class ScaledView : NSView {
let scale: CGFloat
let element: Drawable
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented") // not needed, adding it programmatically
}
init(frame frameRect: NSRect, scale: CGFloat, toDraw element: DoubleValue) {
self.scale = scale
self.element = element;
super.init(frame: frameRect)
self.setFrameSize(CGSize(width: self.bounds.size.width * scale, height: self.bounds.size.height * scale))
}
override func drawRect(dirtyRect: NSRect) {
element.draw(dirtyRect, parent: self)
}
}
最佳答案
你确定你不是要设置边界,而不是框架?更改框架将改变 View 在其父级坐标系中的大小,因此不仅会改变其内容的比例,还会改变其在父 View 中的外观。如果你只是想缩放 View 的内容, View 几何指南说:
To translate or scale the coordinate system, you alter the view's bounds rectangle. Changing the bounds rectangle sets up the basic coordinate system with which all drawing performed by the view begins. Concrete subclasses of NSView typically alter the bounds rectangle immediately as needed in their initWithFrame: methods or upon loading a nib file that contains the view.
关于swift - 设置 NSView 的比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38674901/