Search code examples
iosobjective-cuiimageviewuiimagexcode5

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

Solution

  • 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