以编程方式将NSView添加到NSScrollView

时间:2015-05-04 13:33:16

标签: cocoa nsview nsscrollview

我正在尝试向NSView添加一些NSScrollView

NSView *v = [[NSView alloc] initWithFrame:CGRectMake(10,10,100,100)];

NSTextField *t1 = [[NSTextField alloc] init];
[t1 setStringValue:@"test1"];

NSTextField *t2 = [[NSTextField alloc] init];
[t1 setStringValue:@"test2"];

[v addSubview:t1];
[v addSubview:t2];

[[_scrollView documentView] addSubview:v];

我也试过[_scrollView addSubview:v]

似乎没有任何事情发生,即使这些代码执行没有错误。 NSScrollView似乎是空的。 scrollView IBOutlet已连接。任何想法有什么不对?

2 个答案:

答案 0 :(得分:0)

我已经制作了Swift版本的代码来测试你的问题,它运行良好,没有错误如果NSScrollView,NSView或NSTextField都有一个正确的框架

var scrollView = NSScrollView(frame: CGRectMake(0,0,100,100))
scrollView.backgroundColor = NSColor.blueColor()
var v = NSView(frame: CGRectMake(0,0,100,100))
var t1 = NSTextField(frame: CGRectMake(0,0,50,50))
t1.textColor = NSColor.blackColor()
t1.stringValue = "test"
v.addSubview(t1)
scrollView.addSubview(v)

playground

答案 1 :(得分:0)

以下代码对我有用! 不要忘记为视图添加约束。

class AppDelegate: NSObject, NSApplicationDelegate {

let mainWindow: NSWindow = {
    let window = NSWindow(contentRect: NSMakeRect(10, 10, 700, 700), styleMask: .resizable, backing: .buffered, defer: false)
    window.isOpaque = true
    window.styleMask.insert(.miniaturizable)
    window.styleMask.insert(.titled)
    window.makeKeyAndOrderFront(nil)
    window.title = "My Playground"
    window.isMovableByWindowBackground = true
    return window
}()

let scrollView: NSScrollView = {
    let scrollView = NSScrollView()
    scrollView.hasVerticalScroller = true
    scrollView.translatesAutoresizingMaskIntoConstraints = false
    return scrollView
}()

func applicationDidFinishLaunching(_ aNotification: Notification) {

    let v: NSView = NSView(frame: NSMakeRect(10, 10, 100, 100))

    let t1: NSTextField = NSTextField()
    t1.stringValue = "test1"

    let t2: NSTextField = NSTextField()
    t2.stringValue = "test2"

    v.addSubview(t1)
    v.addSubview(t2)

    t1.topAnchor.constraint(equalTo: v.topAnchor, constant: 50).isActive = true
    t1.leftAnchor.constraint(equalTo: v.leftAnchor, constant: 100).isActive = true
    t1.rightAnchor.constraint(equalTo: v.rightAnchor, constant: -100).isActive = true

    t2.topAnchor.constraint(equalTo: t1.bottomAnchor, constant: 10).isActive = true
    t2.leftAnchor.constraint(equalTo: v.leftAnchor, constant: 100).isActive = true
    t2.rightAnchor.constraint(equalTo: v.rightAnchor, constant: -100).isActive = true

    t1.translatesAutoresizingMaskIntoConstraints = false
    t2.translatesAutoresizingMaskIntoConstraints = false

    v.translatesAutoresizingMaskIntoConstraints = false

    mainWindow.contentView = scrollView

    mainWindow.contentView?.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true

    scrollView.documentView = v
    v.topAnchor.constraint(equalTo: scrollView.topAnchor).isActive = true
    v.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
    v.leftAnchor.constraint(equalTo: scrollView.leftAnchor).isActive = true
    v.rightAnchor.constraint(equalTo: scrollView.rightAnchor).isActive = true
    // Insert code here to initialize your application
}

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