【问题标题】:Swift NSStackView : Learn By Doing. What am I doing wrong?Swift NSStackView:边做边学。我究竟做错了什么?
【发布时间】:2014-09-06 13:57:54
【问题描述】:

如何让我的NSStackView 一个接一个地布置我的视图?我的蓝框在我的红框之上。

import Cocoa

class TestView : NSView{
  override init() {
    super.init(frame: NSRect(x: 0,y: 0,width: 100,height: 100))
  }

  required init(coder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
  }

  override func drawRect(dirtyRect: NSRect) {
    super.drawRect(dirtyRect)
    NSColor.redColor().setFill()
    NSBezierPath.fillRect(self.bounds)
  }
}

class TestView2 : NSView{
  override init() {
    super.init(frame: NSRect(x: 0,y: 0,width: 100,height: 100))
  }

  required init(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

  override func drawRect(dirtyRect: NSRect) {
    super.drawRect(dirtyRect)
    NSColor.blueColor().setFill()
    NSBezierPath.fillRect(self.bounds)
  }
}

class AppDelegate: NSObject, NSApplicationDelegate {

  @IBOutlet weak var window: NSWindow!

  @IBOutlet weak var searchFacetItems: SearchFacetSelectorView!
  @IBOutlet weak var searchFacetHeader: SearchFacetSelectorHeader!
  var content : NSStackView!
  let testView = TestView()
  let testView2 = TestView2()

  func applicationDidFinishLaunching(aNotification: NSNotification?) {
    // Insert code here to initialize your application
    content = NSStackView(frame: window.contentView.bounds)
        content.orientation = NSUserInterfaceLayoutOrientation.Vertical
        content.alignment = NSLayoutAttribute.CenterX

    content.addView(testView, inGravity: NSStackViewGravity.Center)
    content.addView(testView2, inGravity: NSStackViewGravity.Center)


    window.contentView = content!
  }

  func applicationWillTerminate(aNotification: NSNotification?) {
    // Insert code here to tear down your application
  }


}

边读边学

这不是 appkit,但也许我会得到一些线索:HOW TO USE UIVIEWS WITH AUTO LAYOUT PROGRAMMATICALLY

Auto Layout Guide

【问题讨论】:

    标签: macos cocoa swift 2d appkit


    【解决方案1】:

    您应该可以通过调整NSRect 坐标来做到这一点。目前,NSRects 使用相同的精确坐标:

    NSRect(x: 0, y: 0, width: 100, height: 100) 
    

    如果您希望NSView 水平(右侧)位于另一个旁边:

    NSRect(0, 0, 100, 100)    // TestView1
    NSRect(100, 0, 100, 100)  // TestView2
    

    垂直下方:

    NSRect(0, 0, 100, 100)    // TestView1
    NSRect(0, -100, 100, 100) // TestView2
    

    【讨论】:

    • 我之前尝试过,它确实有效。但我希望堆栈视图能够布置它自己管理的视图。
    【解决方案2】:

    视图没有任何描述其首选大小的约束,例如intrinsicContentSizes 或显式约束。并且NSStackView 仅添加约束以将其堆叠视图相对于彼此定位,而不是调整单个视图的大小。

    没有它们,它们在堆叠轴上的大小会变得不明确,并且通常会将所有大小分配给单个视图。在您的示例中,我猜蓝色框没有绘制在红色框的顶部,而只是红色框的高度为 0(并且在蓝色视图之后堆叠)。

    NSButtons 添加到堆栈视图没有这个问题,因为它们确实有一个定义的intrinsicContentSize

    取决于您的视图是什么——它们是否具有内在大小,或者它们是否由内部子视图的约束定义——你要么想要覆盖intrinsicContentSize,要么添加这些约束最终定义了它们的高度。


    编辑: 哦,确保在要添加到堆栈视图的视图上将 translatesAutoresizingMaskIntoConstraints 设置为 false(看起来你不是来自示例代码)。如果没有,您很快就会遇到麻烦,因为自动调整大小的约束试图定位视图并与堆栈视图添加的约束发生冲突。

    【讨论】:

      猜你喜欢
      • 2013-08-06
      • 1970-01-01
      • 2016-07-18
      • 2019-12-23
      • 2014-06-15
      • 1970-01-01
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多