【发布时间】:2016-04-26 20:34:50
【问题描述】:
我有一个带有 CALayer 的 NSView (myImage),contents = 一个 NSImage,所以视图的大小现在已经调整为完全适合 NSImage(大小未知,可能会发生变化)。 这是在另一个 NSView (myContainer) 内部,该 NSView (myContainer) 固定在两个轴上独立展开和收缩。
所以我希望它表现得像一个设置为“按比例缩小”的 NSImageView。在调整大小时在“myContainer”视图的范围内保持完全可见,并且永远不会比我将 NSImage 添加到其 CALayer 时创建的大小更大。
仅在 IB 中可能?不确定,但即使在应用程序运行时混合了 IB 创建的约束和生成和修改的约束,我也无法像 NSImageView 那样工作。请帮忙。
有进展的编辑func newUserImageInputDidArrive() -> Void {
// the real widh and height of the NSImage we will add to layer of NSView
currentImageRealWidth = (mainImageDropZone.image?.size.width)!
currentImageRealHeight = (mainImageDropZone.image?.size.height)!
// set the width of NSView to match above
mainImageView.frame.size.width = currentImageRealWidth
mainImageView.frame.size.height = currentImageRealHeight
// create the layer in the NSView
mainImageView.wantsLayer = true
mainImageView.layer?.borderColor = NSColor.redColor().CGColor
mainImageView.layer?.borderWidth = 5
// so the image added to the layer will resize proportionately within the bounds of the layer
mainImageView.layer?.contentsGravity = kCAGravityResizeAspect
// add the image
mainImageView.layer?.contents = mainImageDropZone.image
// so now NSView's layer has the image and is the size of the origonal image
}
带有图层的 NSView 当前被固定在其父视图的所有侧面。 这给了我与“按比例上下缩放”的 NSImage 视图相同的行为。但我只想“按比例缩小”。 所以我觉得我必须将保持图层的 NSView 的宽度和高度限制为从图层中使用的原始图像中检索到的最大宽度和高度。 但是如何实现这一点是在暗示我。
【问题讨论】:
标签: swift nsview nslayoutconstraint nsimageview