添加到子视图后看不到UIImageView

时间:2014-11-17 06:13:12

标签: ios objective-c uiimageview

我正在尝试使用JSON数据创建视图。JSON数据中的标记名为"类型"

{                    
   "type"=text; 
};
       OR 
{
   "type"=image;
};

我将创建一系列类型细节,并从该数组创建视图。**

float y=10;
 for(ViewTypes *type in TypeArray )
{
  if ([type.typeString isEqualToString:@"image"])
{
      NSlog(@"image view is added");       
      UIImageView *imageView=[[UIImageView alloc]init ]; 
      imageView.frame=CGRectMake(55, y, 150, 5);
      imageView.backgroundColor=[UIColor yellowColor];
      [CustomView addSubview:imageView];

      y+=10;

   }
       else if ([type.typeString isEqualToString:@"text"]){
       NSlog(@"text view is added");       
       UITextView *textView=[[UITextView alloc]init ]; 
       textView.frame=CGRectMake(55, y, 150, 5);
       textView.backgroundColor=[UIColor redColor];
       [CustomView addSubview: textView];

       y+=10;
 }
 }

`loop` and if-else  is working fine ,But only `UITextView` is seen in the view and not the `UIImageView`.

2 个答案:

答案 0 :(得分:0)

UIImage中添加UIImageView以显示它。

if ([type.typeString isEqualToString:@"image"]){
   NSlog(@"image view is added");       
  UIImageView *imageView=[[UIImageView alloc]init ]; 
         imageView.frame=CGRectMake(55, y, 150, 5);
         imageView.backgroundColor=[UIColor yellowColor];
         **[imageView setImage:[UIImage imageNamed:@"imagename.png"]];**
         [CustomView addSubview:imageView];
 y+=10;
}

答案 1 :(得分:0)

试试这个,

 float y=10;
 for(ViewTypes *type in TypeArray ){

  if ([type.typeString isEqualToString:@"image"]){

        NSlog(@"image view is added");       
      UIImageView *imageView=[[UIImageView alloc]init ]; 
             imageView.frame=CGRectMake(55, y, 150, 5);
             imageView.backgroundColor=[UIColor yellowColor];
             [CustomView addSubview:imageView];
             [CustomView bringSubviewToFront:imageView];

     y+=10;

   }else if ([type.typeString isEqualToString:@"text"]){
       NSlog(@"text view is added");       
       UITextView *textView=[[UITextView alloc]init ]; 
             textView.frame=CGRectMake(55, y, 150, 5);
             textView.backgroundColor=[UIColor redColor];
             [CustomView addSubview: textView];
    y+=10;
 }}