Question

I'm creating an iOS app that shows several items of a museum. Each item has a certain position on a map of the museum. Their coordinates are stored in an array. So far, so good. Depending on the item you choose, the position of this item should show up on a map.

I've built the View in Xcode's Interface Builder. I've added an ImageView and set its position to 0 (x+y).

I've added the ImageView as an outlet in my .h file:

@property (weak, nonatomic) IBOutlet UIImageView *myView;

Finally in the Views viewDidLoad()-Method I've tried to change the position of the Image View (containing a kind of "pointer-image") after hours of research (e.g here or here or even here) like this:

[self.myView setFrame:CGRectMake(xCoord, yCoord, self.myView.frame.size.width, self.myView.frame.size.height)];

xCoord and yCoord are float variables containing the coordinates of the item. I am NSLogging the frame's position before and after the setFrame-method and it tells me that the x and y position of the ImageView has changed. But on my Screen it still appears in the top left corner of the screen.

I've already tried some different methods like this one:

CGRect needleOutletFrame = [self.myView frame];
needleOutletFrame.origin.x = xCoord;
needleOutletFrame.origin.y = xCoord;
[self.myView setFrame:needleOutletFrame];

Didn't work either ...

Has anyone got a hint for a solution to my problem?

Cheers, Alex

EDIT:

I've deleted the UIImageView that I had created using Interface Builder and created an UIImageView in ViewWillAppear method by myself:

UIImageView *myView;
myView = [[UIImageView alloc] init];
[self.view addSubview:myView];
myView.image = [UIImage imageNamed:@"ort_nadel_r.png"];
[myView setFrame:CGRectMake(257, 90, 300, 300)];

I don't know why, but finally it works as I want it to! Thanks!

Was it helpful?

Solution

If you already have connected the UIImageView in your view controller then try turning off auto-layout to programmatically modify the frame of it:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top