Adèle Dunand Adèle Dunand - 4 months ago 20x
Objective-C Question

ImageView doesn't show in UIScrollView (objective C)

I'm currently trying to put images in a UIScrollView.
I checked a lot for an answer, but none of the topics in stackoverflow works in my code. I guess I just forgot a detail in it, but can't figure it out.

So, I have in a ViewController.m this code:

- (void)initTransportData
_transportsData = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 654, 414, 82)];
CGRect viewSize = _transportsData.bounds;
UIImageView *imgView = [[UIImageView alloc] initWithFrame:viewSize];
[imgView setImage:[UIImage imageNamed:@"image.png"]];
[_transportsData addSubview:imgView];


My _transportData is defined in my ViewController.h like that:

@property (strong, nonatomic) IBOutlet UIScrollView *transportsData;

When i test it, the scrollView display a black color (i set it in my main.storyboard).

I also have a MapView above my scrollView (don't know if it can help to understand).

PS: I call my intTransportData function below [super viewDidLoad] in the viewDidLoad function.

Does anyone know what is wrong?


You have set frame like (0, 654, 414, 82) that means your y position is 654 of scrollview. Then it is below the screen height.

So , set frame properly.

You need to add that scrollview to main view (self.view).

If you have given outlet then no need to set frame again.

You should use autolayout to manage this things easily.

Your scenario should be like, UIScrollview - UIView - UIImageView and you need to set proper constraint to every views. If you unaware of auto layout then search tutorials on google and first learn it.

Hope this will help :)