【发布时间】:2020-03-06 00:40:47
【问题描述】:
我有一个带有 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,但没有成功。
【问题讨论】:
-
你能展示你在哪里创建父视图以及在哪里添加子视图到父视图的代码吗?
-
嗨,rocky,我刚刚用一张图片编辑了我的帖子,以在界面构建器中显示视图。其他视图不是以编程方式添加的。