UIView

2013-07-09 8 views
11

के उदाहरण के लिए एक यूवीमेज व्यू को एक सबव्यू के रूप में जोड़ना मैं शुरुआती कोड का अभ्यास कर रहा हूं क्योंकि मैं नया हूं और मैंने अभी पूरी तरह से भ्रम में भाग लिया है ... यही वह है जो मेरे पास अब तक हैUIView

UIView *catView = [[UIView alloc] init]; 
UIImage *image = [UIImage imageNamed:@"lolcat.png"]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 
[catView.view addSubview:imageView]; 

मुझे समझ में नहीं आता कि यहां और क्यों कुछ गलत है, क्या कोई मदद कर सकता है?

+0

आप क्या देखते हैं? मुझे लगता है कि आपके 'catView' में शून्य फ्रेम है, इसलिए – Wain

+0

बहुत अच्छी तरह से प्रदर्शित नहीं होगा, जिसे ट्रीहाउस नामक एक लर्निंग साइट की कोड चुनौती का हिस्सा है, इसलिए संपादक ऑनलाइन है और सिमुलेशन नहीं दिखाता है :( –

+0

तो साइट आपको बस कुछ बताती है गलत है और आपको क्या पूछता है? आप कोड को एक प्रोजेक्ट में कॉपी कर सकते हैं और इसे चलाने का प्रयास कर सकते हैं ... – Wain

उत्तर

26
//You need to specify the frame of the view 
UIView *catView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,400)]; 

UIImage *image = [UIImage imageNamed:@"lolcat.png"]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 

//specify the frame of the imageView in the superview , here it will fill the superview 
imageView.frame = catView.bounds; 

// add the imageview to the superview 
[catView addSubview:imageView]; 

//add the view to the main view 

[self.view addSubview:catView]; 
+0

' catView' में 'view' प्रॉपर्टी नहीं है। – rmaddy

+0

ओह हाँ, मैंने देखा कि जब मैंने कोड पर टिप्पणी की तो इसे हटाने के लिए भूल गया – soryngod

0

दिलचस्प, और सूक्ष्म नोट। यदि विचार पहले से ही .xib फ़ाइल में जोड़े गए हैं, तो विचार "कमजोर" हैं और आपको एक अस्थायी चर के साथ स्वैप करने की आवश्यकता है। आपके दृश्य में सेट किए गए मिलान से समन्वय प्राप्त करने के लिए समन्वय प्राप्त करने के लिए कुछ सरल गणित:

@property (weak, nonatomic) IBOutlet UIImageView *imageView1; 
@property (weak, nonatomic) IBOutlet UIImageView *imageView2; 
CGRect tempFrame; 

tempFrame = self.imageView1.frame; 

CGRect tempFrame; // use bounds instead 

tempFrame = self.imageView2.frame; 

__strong UIImageView * tempView = self.imageView2; 
[self.imageView2 willMoveToSuperview: nil]; 
[self.imageView2 removeFromSuperview]; 
[self.imageView2 willMoveToSuperview: self.imageView1]; 
[self.imageViewSkate addSubview: self.imageViewBall]; 
self.imageView2.frame = CGRectMake(tempFrame.origin.x - self.imageView1.frame.origin.x, 
             tempFrame.origin.y - self.imageView1.frame.origin.y, 
             tempFrame.size.width, tempFrame.size.height); 
tempView = nil; 

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