Search code examples
iphoneobjective-ciosxcodeuiinterfaceorientation

Resizing multiple image views with orientation


I'm in the process of creating a universal iOS app that, amongst other functions, allows the user to spawn UIImageViews on touch.

The issue that I'm having is that when I rotate the device, the views that are created do not resize correctly.

I have placed the code into my main view controller method, as shown:

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event
{
 //first part - creating the frame

 view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"MyImage.png"]];
 view.frame = CGRectMake(50, 50, 100, 100);
 [self.view addSubview:view];

 //second part - triggering image placement on touch

 UITouch *touch = [[event allTouches] anyObject];
 view.center = [touch locationInView:self.view];
}

Does anybody know of any way in which I can implement an autoresizing method for the views that are dynamically created?

The effect that I'm looking for is: instead of the images using the old portrait coordinates, they somehow resize with an orientation change into landscape, so that they're all visible in positions that are relative to the portrait ones.

If I were using interface builder then it would be easy enough, but I can't find an obvious solution when doing this programmatically.

It's not as simple as returning YES for the shouldAutorotateToInterfaceOrientation, either.

I've checked a lot of the question/answer resources for iOS development, and not many of the solutions seem to focus on dynamically created UIImageViews and frames such as in my example.

I would be extremely grateful if anybody could take the time to provide me a solution, or even just point me in the right direction.

Thank you in advance,

Rory


Solution

  • set the auto resizing mask accordingly every UIView has a property UIViewAutoresizing

    view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;