我有一个包含3个子视图的自定义视图(NSView),其布局如下:
----------------------------------------------------
bar : always visible. With a button to toggle tnView Fixed height
----------------------------------------------------
tnView : height is 30 if toggled, zero if not. Height = 30 or zero
----------------------------------------------------
PDFView that takes the remaining space to to bottom Flexible height
----------------------------------------------------
问题是由tnView引起的,可以通过按下按钮显示(高度= 30)或隐藏(高度= 0)。它会阻止主视图(上面描述的3的父视图)垂直调整大小
以下是我的ViewController的代码:
override func viewDidLoad() {
super.viewDidLoad()
tnView.autoresizingMask = NSAutoresizingMaskOptions([.viewWidthSizable, .viewHeightSizable, .viewMaxXMargin,.viewMinYMargin,.viewMaxYMargin])
tnView.translatesAutoresizingMaskIntoConstraints = true
// hide view at init
tnView.frame.origin.y += tnViewHeight // constant set to 30
tnView.frame.size.height = 0
tnView.needsDisplay = true
}
// Action connected to the toggle button
@IBAction func openTNView(_ sender: NSButton) {
// should the view be opened or closed?
let isOpenView = self.tnView.frame.size.height == 0
// Create the dictionary for animating the view
var viewDict = [String: Any]()
viewDict[NSViewAnimationTargetKey] = self.tnView
viewDict[NSViewAnimationStartFrameKey] = self.tnView.frame
var endFrame = self.tnView.frame
endFrame.origin.y -= isOpenView ? tnViewHeight : -tnViewHeight
endFrame.size.height = isOpenView ? tnViewHeight : 0
viewDict[NSViewAnimationEndFrameKey] = endFrame
// Create the view animation object
let theAnim = NSViewAnimation(viewAnimations: [viewDict])
theAnim.duration = 0.4 // in seconds
theAnim.start()
if isOpenView {
// isHidden is set to true automatically when resizing to zero => unset the flag
self.tnView.isHidden = false
}
}
问题是主视图无法垂直调整大小(其高度无法更改)。水平都好。我尝试更改autoresizingMask
,但没有成功。
任何的想法?谢谢: - )
编辑:下面是界面构建器中的视图结构。 The View Structure in IB http://img11.hostingpics.net/pics/550625ibstruct.png
答案 0 :(得分:0)
我无法找到解决这个问题的方法,但我找到了使用NSSplitView
的解决方法。这更容易,最终结果完全相同(除了没有错误: - ))。