Reputation: 1304
I have a simple app that displays a UIPageViewController
when a cell is clicked on in a UITableViewController
. The UIPageViewController
(called Leaflets
) shows 6 images in a very good layout for both the iPhone and iPad:
- (void)imageDimensions
{
[self.view setBackgroundColor:[UIColor clearColor]];
CGRect insetFrame;
if (IDIOM == IPAD)
{
NSLog(@"iPad");
insetFrame = CGRectMake(160, 70, self.view.frame.size.width-320, self.view.frame.size.height-115);
}
else
{
NSLog(@"iPhone");
insetFrame = CGRectMake(30, 20, self.view.frame.size.width-60, self.view.frame.size.height-15);
}
_imageView = [[UIImageView alloc] initWithFrame:insetFrame];
[_imageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[_imageView setImage:[UIImage imageNamed:_model.imageName]];
[[self view] addSubview:_imageView];
}
When I rotate the iPad to Landscape
mode, ideally, I want the insetFrame
to change so that the image isn't stretched out across the screen.
I tried by putting a NSNotification
in the viewDidLoad
:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceRotated:) name:UIDeviceOrientationDidChangeNotification object:nil];
Which calls:
-(void)deviceRotated:(NSNotification*)notification
{
NSLog(@"Rotation");
CGRect insetFrame;
insetFrame = CGRectMake(160, 70, self.view.frame.size.width-620, self.view.frame.size.height-715);
_imageView = [[UIImageView alloc] initWithFrame:insetFrame];
[_imageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[_imageView setImage:[UIImage imageNamed:_model.imageName]];
[[self view] addSubview:_imageView];
}
The method here is definitely getting called but there are two issues.
Issues
Without adding the imageView
to the view, nothing changes on the property go the image and if I keep the method how it's displayed above, it adds the image on top of the existing image.
Ideally, I'd like to remove the image from the view and reset it's coordinates for the portrait mode.
[_imageView removeFromSuperview];
If I set that in the deviceRotated
method, it gets called each time the view is rotated, so it's always removing, etc.
I'd like a way to say, if landscape, show this frame and if portrait, show this frame.
Any guidance on this would be appreciated.
Upvotes: 0
Views: 76
Reputation: 425
Have you tried setting the display up for redraw and relayout?
CGRect insetFrame;
insetFrame = CGRectMake(160, 70, self.view.frame.size.width-620, self.view.frame.size.height-715);
_imageView = [[UIImageView alloc] initWithFrame:insetFrame];
[_imageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[_imageView setImage:[UIImage imageNamed:_model.imageName]];
[_imageView setNeedsDisplay];
[_imageView setNeedsLayout];
This might work for you. Remove the [[self view] addSubview:_imageView] line for sure; you're adding it to the display repeatedly.
Upvotes: 1