简体   繁体   English

添加到子视图后看不到UIImageView

[英]UIImageView not seen after adding to subview

I am trying to create Views wrt the JSON data.There is Tag in JSON data called "type" 我试图创建视图WRT的JSON data.There在标签JSON所谓的“类型”数据

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

And I will created array of type details and from that array creating views.** 我将创建类型详细信息数组,并从该数组创建视图。**

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`.

Add UIImage in UIImageView to show it. UIImageView添加UIImage以显示它。

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

Try this, 尝试这个,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM