2012-04-04 13 views
5

Tôi có một lớp NSView sẽ quản lý Chế độ xem tùy chỉnh được tạo trong tệp nib.Tạo NSScrollView Programatically trong một NSView - Cocoa

Bây giờ tôi muốn thêm một NSScrollView vào chế độ xem tùy chỉnh, nhưng tôi cần thực hiện theo chương trình và không sử dụng Trình tạo giao diện (Nhúng vào chế độ xem cuộn).

Tôi đã tìm thấy mã này:

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

Giả sử Tôi tuyên bố như IBOutlets các biến 'MainWindow' và 'scrollview' ở trên, làm thế nào tôi sẽ đi về kết nối chúng với các thành phần thích hợp trong giao diện Builder? Liệu nó có ý nghĩa gì không?

Hoặc có cách nào tốt hơn để thêm chế độ xem cuộn theo chương trình không?

Cảm ơn bạn!

P.S. Tôi không thể kết nối chúng theo cách thông thường vì tôi không thể tạo đối tượng NSObject từ Trình tạo giao diện hoặc sử dụng Chủ sở hữu tệp ..

+0

Nếu bạn tạo ra quan điểm lập trình họ không "kết nối" với giao diện người xây dựng – Otium

+3

gì? Tất nhiên bạn có thể liên quan đến các chế độ xem có lập trình với các chế độ xem được tạo bởi trình tạo giao diện. – ctpenrose

Trả lời

7

Đoạn mã này sẽ chứng minh cách tạo NSScrollView theo chương trình và sử dụng nó để hiển thị bất kỳ chế độ xem nào từ một ngòi hoặc từ mã. Trong trường hợp chế độ xem được tạo ra bằng nib, bạn chỉ cần tải tệp nib vào chế độ xem tùy chỉnh của mình trước đó và có một lối thoát cho chế độ xem tùy chỉnh (outletToCustomViewLoadedFromNib) được tạo cho Chủ sở hữu tệp.

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 có hướng dẫn về chủ đề mà tôi sẽ không liên kết vì nó yêu cầu quyền truy cập của Apple Developer Connection và liên kết của chúng thường xuyên bị gián đoạn. Nó có tiêu đề "Tạo và định cấu hình chế độ xem cuộn" và hiện có thể được tìm thấy bằng cách tìm kiếm tiêu đề của nó bằng cách sử dụng Google.

+0

Cảm ơn bạn rất nhiều! – Kevin

2

Tôi gặp khó khăn khi tạo NSScrollView với AutoLayout theo chương trình, nhưng cuối cùng đã hoạt động. Đây là phiên bản 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

Tôi đã tìm kiếm nội dung này quá lâu! Cảm ơn bạn rất nhiều vì đã cung cấp điều này, đó chính xác là những gì tôi đang tìm kiếm và tiết kiệm cho tôi hàng giờ! – chl