2011-10-30 2 views
2

J'ai une application où il lit un document XML contenant 2 champs (image URL et description). Si le document a un champ qui a une URL d'image valide, je veux que l'affichage montre l'image. Sinon, je veux juste qu'il montre la description.Comment ajouter dynamiquement un UIImageView à une vue et positionner des éléments?

Le problème que j'ai est:

  1. comment afficher la UIImageView dynamique
  2. comment déplacer le UITextView vers le bas depuis maintenant j'ai ajouté un UIImageView

Actuellement, je viens d'une Voir avec un UITextView dessus.

Des idées?

Répondre

2

1) -[UIView setHidden:] si le UIImageView est déjà en place. Sinon, [[UIImageView alloc] initWithFrame:], puis ajoutez l'affichage de l'image en tant que sous-vue à la vue.

2) -[UIView setFrame:] (est une option)

1

Voici ce que vous pourriez faire.

Préparer la vue avec le UIImageView et le UITextView dans le viewDidLoad comme ceci:

-(void) viewDidLoad) 
{ 
    [super viewDidLoad]; 
    myImageView = [[UIImageView alloc] init]; 
    myImageView.frame = CGRectMake(x,y,0,0); //you can set it at the right position and even set either the width OR the height depending on where you want the textView in my example I'm gonna assume its beneath the imageView 
    //other imageView settings 
    [myView addSubView:myImageView]; 

    myTextView = [[myTextView alloc] init]; 
    myTextView.frame = CGRectMake(x,y,width,heigh+CGRectGetMaxY(myImageView.frame)); //here you prepare the textView for the imageView and the space it takes. But doesn't get hindered by it if it's not there. 
    //other textView settings. 
    [myView addSubView:myTextView]; 

    //You would want this loading function about here OR after this loading has been occured. 
    [self loadImageFromURL]; 
} 

-(void)loadImageFromURL 
{ 
    //get your image here.. or not 

    if(imageHasLoaded) //some condition that gets set when your image has successfully loaded. (This should be done in a delegate (didLoadImageFromURL) if you have such thing prepared. 
    { 
     //myImageView.image = theLoadedImage; 
     myImageView.frame = CGRectMake(x,y,theLoadedImageWidth,theLoadedImageHeight); 
     //after you set the frame of the imageView you need to refresh the view 
     [myView setNeedsDisplay]; 
    } 
} 

Et voici comment vous devez ajouter dynamiquement une imageView avec l'image et le cadre pour une vue.

Questions connexes