2012-04-04 79 views
5

我有一個NSView類,它負責在nib文件中創建的自定義視圖。以編程方式在NSView中創建NSScrollView - Cocoa

現在我想添加一個NSScrollView到自定義視圖,但我需要以編程方式執行它,而不是使用Interface Builder(嵌入到滾動視圖)。

我發現這個代碼:

NSView *windowContentView = [mainWindow contentView]; 
NSRect windowContentBounds = [windowContentView bounds]; 
scrollView = [[NSScrollView alloc] init]; 
[scrollView setBorderType:NSNoBorder]; 
[scrollView setHasVerticalScroller:YES]; 
[scrollView setBounds: windowContentBounds]; 
[windowContentView addSubview:scrollView]; 

假設我宣佈作爲IBOutlets變量「主窗口」和「滾動視圖」上面,我將如何去把它們連接到在Interface Builder中適當的組件?這樣做是否有意義?

或者有沒有更好的方式來以編程方式添加滾動視圖?

謝謝!

P.S.我無法以通常的方式連接它們,因爲我無法通過Interface Builder創建NSObject對象,或者使用文件所有者。

+0

如果您創建視圖編程它們不與界面構建 – Otium 2012-04-04 18:22:59

+3

什麼「連接」?當然,您可以將編程視圖與界面生成器生成的視圖相關聯。 – ctpenrose 2012-04-05 05:23:17

回答

7

此代碼片段應演示如何以編程方式創建NSScrollView並使用它來顯示任何視圖從筆尖或代碼。在生成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]; 

蘋果有一個關於此主題的指南,我不會鏈接到它,因爲它需要Apple Developer Connection訪問權限,並且它們的鏈接頻繁斷開。它的標題是「創建和配置滾動視圖」,目前可以通過使用Google搜索其標題來找到。

+0

非常感謝! – Kevin 2012-04-05 07:19:15

2

我很難以編程方式創建NSScrollViewAutoLayout,但終於得到它的工作。這是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)) 

enter image description here

+1

我已經搜索了這麼久!非常感謝你提供這些,這正是我一直在尋找的東西,並節省了我的時間! – chl 2018-01-22 11:27:27