2012-04-04 14 views
5

मेरे पास एक एनएसवी व्यू क्लास है जो निब फ़ाइल में बनाए गए कस्टम व्यू का ख्याल रखती है।एनएसवी व्यू में प्रोग्रामेटिक रूप से एनएसएसक्रॉल व्यू बनाएं - कोको

अब मैं कस्टम दृश्य में एक NSScrollView जोड़ना चाहता हूं, लेकिन मुझे इसे प्रोग्रामेटिक रूप से करने की आवश्यकता है और इंटरफ़ेस बिल्डर (स्क्रॉल व्यू में एम्बेड करें) का उपयोग नहीं करना है।

मैं इस कोड पाया है:

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 के रूप में घोषित, कैसे मैं उन्हें इंटरफ़ेस बिल्डर में उचित घटकों से कनेक्ट करने के बारे में जाना होगा? क्या यह इस तरह से करने का कोई मतलब है?

या क्या स्क्रॉल व्यू प्रोग्रामेटिक रूप से जोड़ने का एक बेहतर तरीका है?

धन्यवाद!

पीएस मैं उन्हें सामान्य तरीके से कनेक्ट नहीं कर सकता क्योंकि मैं इंटरफ़ेस बिल्डर से एनएसओब्जेक्ट ऑब्जेक्ट नहीं बना सकता, या फ़ाइल स्वामी का उपयोग नहीं कर सकता ..

+0

आप दृश्य बना तो करते प्रोग्राम के रूप में वे इंटरफ़ेस बिल्डर – Otium

+3

क्या के साथ "कनेक्ट" नहीं? बेशक आप इंटरफ़ेस बिल्डर जेनरेट किए गए दृश्यों के साथ प्रोग्रामेटिक दृश्यों को जोड़ सकते हैं। – ctpenrose

उत्तर

7

यह कोड खंड दिखाता है कि एनएसएसक्रॉल व्यू प्रोग्रामेटिक रूप से कैसे बनाएं और किसी भी दृश्य को प्रदर्शित करने के लिए इसका उपयोग करें, चाहे एक नीब या कोड से। निब जनरेट किए गए दृश्य के मामले में, आपको बस अपने कस्टम व्यू में nib फ़ाइल लोड करने की आवश्यकता है, और फ़ाइल के स्वामी को आपके कस्टम व्यू (आउटलेट टू कस्टम व्यूलोडेडफ्रॉमनिब) के लिए आउटलेट रखना होगा। के रूप में यह आवश्यकता है Apple डेवलपर कनेक्शन का उपयोग और अपने लिंक अक्सर तोड़ने

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]; 

एप्पल विषय है, जो मैं करने के लिए लिंक नहीं किया जाएगा पर एक गाइड है। इसका शीर्षक "स्क्रॉल व्यू बनाना और कॉन्फ़िगर करना" है और वर्तमान में Google का उपयोग करके इसके शीर्षक की खोज करके पाया जा सकता है।

+0

बहुत बहुत धन्यवाद! – Kevin

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

संबंधित मुद्दे