Cocoa Touch - Adding a UIImageView programmatically?

后端 未结 4 1853
小蘑菇
小蘑菇 2020-12-13 15:12

How can I create and position a new imageview in objective-c?

Thanks!

I tried this but it doesnt seem to do anything...

-(void)drawStars{ //u         


        
相关标签:
4条回答
  • 2020-12-13 15:31

    You are asking about iPhone image view. Right? Then try this

    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
    

    This will create an image view of 100 x 50 dimension and locating (10, 10) of parent view.

    Check the reference manual for UIImageView and UIView. You can set an image by using image property of imageView.

    imgView.image = [UIImage imageNamed:@"a_image.png"];

    And you can use the contentMode property of UIView to configure how to fit the image in the view. For example you can use

    imgView.contentMode = UIViewContentModeCenter
    to place the desired image to the center of the view. The available contentModes are listed here

    0 讨论(0)
  • 2020-12-13 15:42

    You haven't added your view as a subview to another view, meaning it isn't in the view hierarchy.

    Assuming you are doing this in a view controller, it might look something like:

    [self.view addSubview: imgView];
    
    0 讨论(0)
  • 2020-12-13 15:49

    I had problem with passing the value NSSArray to NSString,

    I got the answer:

    //Display all images....
        NSString *imgstring= [self.allimages objectAtIndex:indexPath.row];
        cell.myimages.image=[UIImage imageNamed:imgstring];     //displaying Images in UIImageView..noproblem.
    
    // Display all numbers...
        cell.mylables.text=[NSString stringWithFormat:@"%@", [mysetobjectAtIndex:indexPath.row ]];  //showing results fine ArghyA..
    
    0 讨论(0)
  • 2020-12-13 15:51
    (void)viewDidLoad {
       [super viewDidLoad];
       UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.layer.frame.size.width, self.view.layer.frame.size.height)];
       imgView.image = [UIImage imageNamed:@"emptyCart.jpeg"];
       imgView.backgroundColor = [UIColor whiteColor];
       imgView.contentMode = UIViewContentModeCenter;
       [self.view addSubview: imgView];
    }
    
    0 讨论(0)
提交回复
热议问题