Armand
Armand

Reputation: 433

UIImageView (created programmatically) is not showing up

I am trying to add an UIImageView programatically to my modal view controller and the image simply does not show up. I only see the white background. The rest is working fine as I'm able to load or dismiss the view. My code below:

[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];

UIImageView *imageView = [[[UIImageView alloc] init] initWithFrame:CGRectMake(100, 200, 100, 100)];
imageView.image = [UIImage imageNamed:@"IMG_0352.PNG"];
[self.view addSubview:imageView];


UISwipeGestureRecognizer *swipeleft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
swipeleft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeleft];

UISwipeGestureRecognizer *swiperight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
swiperight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swiperight];


UIBarButtonItem *dismiss_btn = [[UIBarButtonItem alloc] initWithTitle:@"Start App" style:UIBarButtonItemStylePlain target:self action:@selector(dismissModal:)];

self.navigationItem.rightBarButtonItems = [NSMutableArray arrayWithObjects:dismiss_btn, nil];

Upvotes: 1

Views: 1106

Answers (3)

Shruti
Shruti

Reputation: 131

The imageView is getting initialized twice. Try to remove one init

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];

Also while adding the images to the image folder check if the image/ folder is added as a "create group" option in Xcode and not with "create folder reference". Folder reference will just give a link or reference, so you cannot actually access the elements in the folder.

Upvotes: 0

staticVoidMan
staticVoidMan

Reputation: 20274

you need to drop

UIImageView *imageView = [[[UIImageView alloc] init] initWithFrame:CGRectMake(100, 200, 100, 100)];

because... well... init] initWithFrame: looks like a typo and the following is a better way to init a UIImageView:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"IMG_0352.PNG"]];

ref: UIImageView only displays when I call initWithImage

Upvotes: 0

rmaddy
rmaddy

Reputation: 318924

One serious problem is this line:

UIImageView *imageView = [[[UIImageView alloc] init] initWithFrame:CGRectMake(100, 200, 100, 100)];

It needs to be:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 100, 100)];

You had an extra call to init.

Also be sure the filename is really named IMG_0352.PNG. Case matters. Make sure it's not really IMG_0352.png or something similar.

And of course be sure you actually have such an image being packed into your app. Make sure it is listed under the "Copy Bundle Resources" section of the "Build Phases" for your target.

Upvotes: 3

Related Questions