Reputation: 6676
I am really struggling trying to figure out how to handle rotation. I have read numerous posts on here but cannot get items to add to a landscape screen the correct way up and in the correct position.
For instance, I have a portrait view (320x480) and create a big button at position (0,0). I expect that to appear in the top-left corner.
When I rotate the device clock-wise to landscape orientation (480x320), I expect coordinates (0,0) to also be in the top-left corner - but they are not. My button incorrectly appears at the top-right and the text on the button goes down the screen instead of across the landscape/widest direction.
I'll attach my (messy) code in the hope that it sheds some light on what I am trying to do.
Am I wrong in assuming that I can rotate the coordinate system with the device so that the coordinate system becomes 480x320 in landscape mode with 0,0 at the top left? If I'm not wrong.. how do I achieve this?
#include "ContainerVC.h"
@interface UIApplication (AppDimensions)
+(CGSize) currentSize;
+(CGSize) sizeInOrientation:(UIInterfaceOrientation)orientation;
@end
@implementation UIApplication (AppDimensions)
+(CGSize) currentSize
{
return [UIApplication sizeInOrientation:[UIApplication sharedApplication].statusBarOrientation];
}
+(CGSize) sizeInOrientation:(UIInterfaceOrientation)orientation
{
CGSize size = [UIScreen mainScreen].bounds.size;
UIApplication *application = [UIApplication sharedApplication];
if( UIInterfaceOrientationIsLandscape( orientation ) )
{
size = CGSizeMake(size.height, size.width);
}
if (application.statusBarHidden == NO)
size.height -= MIN(application.statusBarFrame.size.width, application.statusBarFrame.size.height);
return size;
}
@end
@interface ContainerVC ()
@end
@implementation ContainerVC
int g_MaxPixelHeight;
int g_MaxPixelWidth;
-(void) addMyContent
{
// TEST BUTTON !!
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake( 0, 0, 200, 200 );
[button setBackgroundColor: [UIColor redColor]];
// Configure title(s)
button.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
button.titleLabel.textAlignment = UITextAlignmentCenter;
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleShadowColor:[UIColor colorWithRed:.25 green:.25 blue:.25 alpha:1] forState:UIControlStateNormal];
[button setTitleShadowOffset:CGSizeMake(0, -1)];
[button setFont:[UIFont boldSystemFontOfSize:40]];
[button setTitle: @"TITLE" forState:UIControlStateNormal];
[self.view addSubview:button];
return;
}
-(void) loadView
{
// PDS> Stack overflow says don't call this but I get errors if I don't!!
[super loadView];
}
-(void) viewDidLoad
{
NSLog( @"** VIEW DID LOAD" );
[super viewDidLoad];
g_ContainerVC = self;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector( didRotate: ) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
}
- (BOOL)canBecomeFirstResponder
{
return YES;
}
-(void) viewDidAppear:(BOOL)animated
{
NSLog( @"** VIEW DID APPEAR" );
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
-(void) viewWillDisappear:(BOOL)animated
{
[self resignFirstResponder];
[super viewWillDisappear:animated];
}
-(void) viewWillAppear: (BOOL) animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector( orientationChanged: ) name:UIDeviceOrientationDidChangeNotification object:nil];
}
-(void) viewDidDisappear:(BOOL) animated
{
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}
-(void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation
{
}
-(void) orientationChanged: (NSNotification *) notification
{
CGSize screenSize = [UIApplication currentSize];
NSLog( @"** orientationChanged ** ScreenSize: %d x %d", (int) screenSize.width, (int) screenSize.height );
g_MaxPixelWidth = screenSize.width;
g_MaxPixelHeight = screenSize.height;
return;
}
-(void) didRotate: (id) sender
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
UIInterfaceOrientation cachedOrientation = [self interfaceOrientation];
if( orientation == UIDeviceOrientationUnknown ||
orientation == UIDeviceOrientationFaceUp ||
orientation == UIDeviceOrientationFaceDown )
{
orientation = (UIDeviceOrientation)cachedOrientation;
}
if( orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight )
{
if( orientation == UIDeviceOrientationLandscapeLeft )
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft;
else
if( orientation == UIDeviceOrientationLandscapeRight )
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
CGSize screenSize = [UIApplication currentSize];
g_MaxPixelWidth = screenSize.width;
g_MaxPixelHeight = screenSize.height;
NSLog( @"** didRotate: LANDSCAPE: %d x %d", g_MaxPixelWidth, g_MaxPixelHeight );
}
if( orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown )
{
if( orientation == UIDeviceOrientationPortrait )
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;
else
if( orientation == UIDeviceOrientationPortraitUpsideDown )
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortraitUpsideDown;
CGSize screenSize = [UIApplication currentSize];
g_MaxPixelWidth = screenSize.width;
g_MaxPixelHeight = screenSize.height;
NSLog( @"** didRotate: PORTRAIT: %d x %d", g_MaxPixelWidth, g_MaxPixelHeight );
}
for (UIView *view in self.view.subviews)
{
[view removeFromSuperview];
}
}
-(void) layoutSubviews
{
NSLog( @"- layoutSubviews" );
}
- (void) viewWillLayoutSubviews
{
NSLog( @"- viewWillLayoutSubviews" );
[self addMyContent];
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
@end
Upvotes: 1
Views: 1430
Reputation: 6676
I think this may be the answer - using the CGAffineTransformMakeRotation method..
Indeed, this has solved my problem.
Upvotes: 0