Objective c 在NSView-Cocoa中以编程方式创建NSScrollView

Objective c 在NSView-Cocoa中以编程方式创建NSScrollView,objective-c,cocoa,nsscrollview,Objective C,Cocoa,Nsscrollview,我有一个NSView类,它负责在nib文件中创建的自定义视图 现在我想在自定义视图中添加一个NSScrollView,但我需要以编程方式进行,而不是使用Interface Builder(嵌入到Scroll视图中) 我发现了以下代码: NSView *windowContentView = [mainWindow contentView]; NSRect windowContentBounds = [windowContentView bounds]; scrollView = [[NSScro

我有一个NSView类,它负责在nib文件中创建的自定义视图

现在我想在自定义视图中添加一个NSScrollView,但我需要以编程方式进行,而不是使用Interface Builder(嵌入到Scroll视图中)

我发现了以下代码:

NSView *windowContentView = [mainWindow contentView];
NSRect windowContentBounds = [windowContentView bounds];
scrollView = [[NSScrollView alloc] init];
[scrollView setBorderType:NSNoBorder];
[scrollView setHasVerticalScroller:YES];
[scrollView setBounds: windowContentBounds];
[windowContentView addSubview:scrollView];
假设我将上面的变量“mainWindow”和“scrollView”声明为IBOutlets,我将如何将它们连接到Interface Builder中的适当组件?这样做有意义吗

还是有更好的方法以编程方式添加滚动视图


另外,我无法以常规方式连接它们,因为我无法从Interface Builder创建NSObject对象,也无法使用文件所有者。

此代码片段应演示如何以编程方式创建NSScrollView,并使用它显示任何视图,无论是从nib还是从代码。对于nib生成的视图,您只需先将nib文件加载到自定义视图中,然后向文件所有者创建自定义视图的出口(outletToCustomViewLoadedFromNib)

NSScrollView *scrollView = [[NSScrollView alloc] initWithFrame:[[mainWindow contentView] frame]];

// configure the scroll view
[scrollView setBorderType:NSNoBorder];
[scrollView setHasVerticalScroller:YES];

// embed your custom view in the scroll view
[scrollView setDocumentView:outletToCustomViewLoadedFromNib];

// set the scroll view as the content view of your window
[mainWindow setContentView:scrollView];

苹果有一个关于这个主题的指南,我不会链接到它,因为它需要苹果开发者的连接访问,而且他们的链接经常中断。它的标题是“创建和配置滚动视图”,目前可以通过谷歌搜索它的标题来找到。

我很难用
AutoLayout
以编程方式创建
NSScrollView
,但最终还是成功了。这是
Swift
版本

    // Initial scrollview
    let scrollView = NSScrollView()
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    scrollView.borderType = .noBorder
    scrollView.backgroundColor = NSColor.gray
    scrollView.hasVerticalScroller = true

    window.contentView?.addSubview(scrollView)
    window.contentView?.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[scrollView]|", options: [], metrics: nil, views: ["scrollView": scrollView]))
    window.contentView?.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[scrollView]|", options: [], metrics: nil, views: ["scrollView": scrollView]))

    // Initial clip view
    let clipView = NSClipView()
    clipView.translatesAutoresizingMaskIntoConstraints = false
    scrollView.contentView = clipView
    scrollView.addConstraint(NSLayoutConstraint(item: clipView, attribute: .left, relatedBy: .equal, toItem: scrollView, attribute: .left, multiplier: 1.0, constant: 0))
    scrollView.addConstraint(NSLayoutConstraint(item: clipView, attribute: .top, relatedBy: .equal, toItem: scrollView, attribute: .top, multiplier: 1.0, constant: 0))
    scrollView.addConstraint(NSLayoutConstraint(item: clipView, attribute: .right, relatedBy: .equal, toItem: scrollView, attribute: .right, multiplier: 1.0, constant: 0))
    scrollView.addConstraint(NSLayoutConstraint(item: clipView, attribute: .bottom, relatedBy: .equal, toItem: scrollView, attribute: .bottom, multiplier: 1.0, constant: 0))

    // Initial document view
    let documentView = NSView()
    documentView.translatesAutoresizingMaskIntoConstraints = false
    scrollView.documentView = documentView
    clipView.addConstraint(NSLayoutConstraint(item: clipView, attribute: .left, relatedBy: .equal, toItem: documentView, attribute: .left, multiplier: 1.0, constant: 0))
    clipView.addConstraint(NSLayoutConstraint(item: clipView, attribute: .top, relatedBy: .equal, toItem: documentView, attribute: .top, multiplier: 1.0, constant: 0))
    clipView.addConstraint(NSLayoutConstraint(item: clipView, attribute: .right, relatedBy: .equal, toItem: documentView, attribute: .right, multiplier: 1.0, constant: 0))

    // Subview1
    let view1 = NSView()
    view1.translatesAutoresizingMaskIntoConstraints = false
    view1.wantsLayer = true
    view1.layer?.backgroundColor = NSColor.red.cgColor
    documentView.addSubview(view1)
    documentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[view1]|", options: [], metrics: nil, views: ["view1": view1]))

    // Subview2
    let view2 = NSView()
    view2.translatesAutoresizingMaskIntoConstraints = false
    view2.wantsLayer = true
    view2.layer?.backgroundColor = NSColor.green.cgColor
    documentView.addSubview(view2)
    documentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[view2]|", options: [], metrics: nil, views: ["view2": view2]))

    // Subview3
    let view3 = NSView()
    view3.translatesAutoresizingMaskIntoConstraints = false
    view3.wantsLayer = true
    view3.layer?.backgroundColor = NSColor.blue.cgColor
    documentView.addSubview(view3)
    documentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[view3]|", options: [], metrics: nil, views: ["view3": view3]))

    // Vertical autolayout
    documentView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[view1(==100)][view2(==200)][view3(==300)]", options: [], metrics: nil, views: ["view1": view1, "view2": view2, "view3": view3]))
    documentView.addConstraint(NSLayoutConstraint(item: documentView, attribute: .bottom, relatedBy: .equal, toItem: view3, attribute: .bottom, multiplier: 1.0, constant: 0))

Brian的回答是正确的,下面是如何在Swift 4.2中创建
NSStackView
内部
NSScrollView

您可能需要翻转
NSClipView

final class FlippedClipView: NSClipView {
  override var isFlipped: Bool {
    return true
  }
}


private func setup() {
  setupScrollView()
  setupStackView()
}

private func setupScrollView() {
  view.addSubview(scrollView)
  scrollView.translatesAutoresizingMaskIntoConstraints = false
  scrollView.hasVerticalScroller = true
  scrollView.drawsBackground = false

  NSLayoutConstraint.activate([
    scrollView.leftAnchor.constraint(equalTo: view.leftAnchor),
    scrollView.rightAnchor.constraint(equalTo: view.rightAnchor),
    scrollView.topAnchor.constraint(equalTo: view.topAnchor),
    scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -30),

    scrollView.heightAnchor.constraint(equalToConstant: 400)
  ])

  let clipView = FlippedClipView()
  clipView.drawsBackground = false
  scrollView.contentView = clipView
  clipView.translatesAutoresizingMaskIntoConstraints = false
  NSLayoutConstraint.activate([
    clipView.leftAnchor.constraint(equalTo: scrollView.leftAnchor),
    clipView.rightAnchor.constraint(equalTo: scrollView.rightAnchor),
    clipView.topAnchor.constraint(equalTo: scrollView.topAnchor),
    clipView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor)
  ]
  scrollView.documentView = stackView

  stackView.translatesAutoresizingMaskIntoConstraints = false
  NSLayoutConstraint.activate([
    stackView.leftAnchor.constraint(equalTo: clipView.leftAnchor),
    stackView.topAnchor.constraint(equalTo: clipView.topAnchor),
    stackView.rightAnchor.constraint(equalTo: clipView.rightAnchor),
    // NOTE: No need for bottomAnchor
  ])
}

private func setupStackView() {
  stackView.orientation = .vertical
  stackView.edgeInsets = NSEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)

  NSLayoutConstraint.activate([
    myRowView.heightAnchor.constraint(equalToConstant: 40)
  ])

  myRowView.onPress = { [weak self] in
    self?.doSomething()
  }

  stackView.addArrangedSubview(myRowView)
}

下面是一个以编程方式在
NSScrollView
内部
NSStackView
的示例

我用它来保持简洁

// Create NSScrollView and add it as a subview in desired location
let scrollView = NSScrollView()
scrollView.borderType = .noBorder
scrollView.verticalScrollElasticity = .none
addSubview(scrollView)
scrollView.snp.makeConstraints { $0.edges.equalToSuperview() }

// Assign an instance of NSClipView to `contentView` property of `NSScrollView`
let clipView = NSClipView()
scrollView.contentView = clipView
clipView.snp.makeConstraints { $0.edges.equalTo(scrollView) }

// Assign whatever view you want to put inside scroll view
// to the `documentView` property. All note the constraints added.
// I have added just 3 constraints
scrollView.documentView = stackView
stackView.snp.makeConstraints { $0.top.bottom.left.equalTo(clipView) }

请注意,在将视图添加为子视图后,我没有使用translatesAutoresizingMaskIntoConstraints,因为snapkit在内部处理它


如果未使用snapkit,则必须在子视图上执行转换自动调整大小GMaskintoConstraints

如果以编程方式创建视图,则这些视图不会与接口生成器“连接”什么?当然,您可以将编程视图与interface builder生成的视图关联起来。我已经搜索了这么久了!非常感谢你提供了这个,这正是我所寻找的,并节省了我的时间!