简体   繁体   English

设置NSView的比例

[英]Set scale of NSView

I subclasses NSView to get it scalable. 我将NSView子类化以使其具有可伸缩性。 Following Apples ' View Geometry ' guide, the bounds should stretch to fit the frame. 按照Apple的“ View Geometry ”指南,边界应延伸以适合框架。 When setting the frame size, nothing changes. 设置帧大小时,没有任何变化。 As I printed the frame size to console, I saw that setFrameSize is doing literally nothing. 当我将帧大小打印到控制台时,我看到setFrameSize实际上什么也不做。

So here my question: How can I change set frame size (without setting set bounds size)? 所以这是我的问题:如何更改设置的帧大小(不设置设置的边界大小)? (Autolayout disabled) (禁用自动版式)

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)
    }
}

Are you sure you don't mean to set the bounds, not the frame? 您确定不是要设置边界,而不是框架吗? Altering the frame will change the view's size in its parent's coordinate system, thus changing not just its contents' scale, but also its appearance within its superview. 更改框架将更改其父级坐标系中视图的大小,从而不仅更改其内容的比例,还更改其在父视图中的外观。 If you just mean to scale the view's contents, the View Geometry guide says: 如果您只是想缩放视图的内容,则“ View Geometry”指南显示:

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. NSView的具体子类通常会在其initWithFrame:方法中或加载包含视图的nib文件时根据需要立即更改bounds矩形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM