如何在OS X应用程序中限制第二个NSViewController的最小大小? [英] How to constrain second NSViewController minimum size in OS X app?
问题描述
我是Mac OS X新手开发人员.我认为这是一个简单的问题,但是我无法通过搜索找到任何有用的结果.
I am a novice Mac OS X developer. I assume this is an easy question, but I haven't been able to find any useful results via searches.
如何限制SECOND视图控制器的大小?
How do I constrain the size of a SECOND view controller?
我从一个简单的Mac OS X应用程序开始,该应用程序只有一个View Controller.我可以选择包含视图控制器的窗口,然后选择大小检查器"并选中最小内容大小"框,然后为该窗口指定最小x和y.
I started with a simple Mac OS X app, with a single View Controller. I can select the window that contains the View Controller, then select the "Size Inspector" and check the "Minimum Content Size" box, and specify a minimum x and y for the window.
这使我能够按预期指定此第一个视图控制器的最小大小.一切都很好.
This allows me to specify the minimum size for this first view controller, as I expect. All is good.
然后,我添加第二个视图控制器,其中第一个视图控制器的模态选择是由按钮按下触发的.我将NSTextView添加到第二个视图控制器,以显示属性字符串.文本视图工作正常,可以正确显示属性字符串.此文本视图是一个单独的窗口,没有最小大小限制.
Then I add a second view controller, with a Modal segue from the first view controller, triggered by a button press. I add a NSTextView to this second view controller, to display an attributed string. The text view works fine, displaying the attributed string correctly. This text view is a separate window, and has no minimum size constraint.
那么我如何为第二个视图控制器视图指定最小大小?这通常是在Interface Builder中完成还是以编程方式完成?当我使用文档大纲"逐步浏览视图层次结构时,看不到如何使用尺寸检查器"指定最小尺寸.我想念什么吗?
So how do i specify the minimum size for this second view controller view? Is this typically done in Interface Builder, or programmatically? When I step through the view hierarchy using Document Outline, I don't see how I can specify minimum size using the Size Inspector. Am I missing something??
这是我的简化代码:
文件"ViewController.swift"
file "ViewController.swift"
class ViewController: NSViewController {
...
override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
let secondVC: SecondViewController = segue.destinationController as! SecondViewController
secondVC.reportAttrString = myReport.reportText
}
...
}
文件"SecondViewController.swift"
file "SecondViewController.swift"
class SecondViewController: NSViewController {
var reportAttrString = NSMutableAttributedString()
@IBOutlet var ReportTextView: NSTextView!
}
我将不胜感激任何建议或指向可能对我有帮助的任何文档或教程的指针.
I would appreciate any suggestions, or pointers to any documentation or tutorials that may help me.
推荐答案
最简单的方法是:
class SecondViewController: NSViewController, NSWindowDelegate {
override func viewWillAppear() {
super.viewWillAppear()
self.view.window?.delegate = self
self.view.window?.minSize = NSSize(width: 100, height: 100)
}
}
override func viewDidAppear() {
super.viewDidAppear()
var frame = self.view.window!.frame
var initialSize = NSSize(width: 100, height: 100)
frame.size = initialSize
self.view.window?.setFrame(frame, display: true)
}
尽管您正在寻找手动方法,但以下方法同样适用.
Although if you were looking for a manual approach then the following would work aswell.
class SecondViewController: NSViewController, NSWindowDelegate {
override func viewWillAppear() {
super.viewWillAppear()
self.view.window?.delegate = self
// Set the initial size
var frame = self.view.window?.frame
var initialSize = NSSize(width: 100, height: 100)
frame.size = initialSize
self.view.window?.setFrame(frame, display: true)
}
func windowWillResize(_ sender: NSWindow, to frameSize: NSSize) -> NSSize {
let minimumSize = NSSize(width: 100, height: 100)
var newSize = NSSize()
if(frameSize.width < minimumSize.width) {
newSize.width = minimumSize.width
} else {
newSize.width = frameSize.width
}
if(frameSize.height < minimumSize.height) {
newSize.height = minimumSize.height
} else {
newSize.height = frameSize.height
}
return newSize
}
}
进一步阅读:
这篇关于如何在OS X应用程序中限制第二个NSViewController的最小大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!