我真的很努力想弄清楚如何处理旋转。我在这里阅读了许多帖子,但无法以正确的方式和正确的位置将项目添加到横向屏幕。
例如,我有一个纵向视图 (320x480) 并在位置 (0,0) 创建一个大按钮。我希望它出现在左上角。
当我将设备顺时针旋转到横向 (480x320) 时,我希望坐标 (0,0) 也位于左上角 - 但它们不是。我的按钮错误地出现在右上角,并且按钮上的文本在屏幕下方而不是横向/最宽方向。
我将附上我的(凌乱的)代码,希望它能够阐明我正在尝试做的事情。
假设我可以使用设备旋转坐标系,以便坐标系在横向模式下变为 480x320,左上角为 0,0,我错了吗?如果我没记错的话..我该如何做到这一点?
#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