2016-12-20 42 views
0

我有一个自定义视图(的NSView)与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

你可以显示代码在哪里创建父视图,并将子视图添加到父视图的位置? – rocky

+0

嗨洛基,我正确地编辑我的文章与图像显示在界面生成器的意见。其他视图不以编程方式添加。 – vomi

回答

0

我找不到解决办法,但我找到了解决方法,使用NSSplitView。这很容易,最终结果是完全一样的(除了没有错误:-))。