私は iOS 用のアプリを作成しており、ランドスケープ ビューを実装しようとしています。iOS 5 と iOS 6 の両方をサポートしようとしているので、自動レイアウトを無効にしています。横向きに回転すると、レイアウトが正しく読み込まれます。次に横向きに回転し、縦向きに戻すと、ロゴを含む UIImageView がボタンと別のラベルの下に伸びます。
ViewController.m から:
@interface ViewController ()
- (IBAction)showMenu:(id)sender;
@end
@implementation ViewController
@synthesize start, menuButton, vector_status, login_status, items, recordLength, countdown, change_name, iapi, running, timeOver, setupDone, dfm, motionmanager, locationManager, recordDataTimer, timer, testLength, expNum, sampleInterval, sessionName,geoCoder,city,country,address,dataToBeJSONed,elapsedTime,recordingRate, experiment,firstName,lastInitial,userName,useDev,passWord,session_num,managedObjectContext,dataSaver,x,y,z,mag,image ;
// displays the correct xib based on orientation and device type - called automatically upon view controller entry
-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if([UIDevice currentDevice].userInterfaceIdiom==UIUserInterfaceIdiomPad) {
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
[[NSBundle mainBundle] loadNibNamed:@"ViewController~landscape_iPad"
owner:self
options:nil];
[self viewDidLoad];
} else {
[[NSBundle mainBundle] loadNibNamed:@"ViewController_iPad"
owner:self
options:nil];
[self viewDidLoad];
}
} else {
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
[[NSBundle mainBundle] loadNibNamed:@"ViewController~landscape_iPhone"
owner:self
options:nil];
[self viewDidLoad];
} else {
[[NSBundle mainBundle] loadNibNamed:@"ViewController_iPhone"
owner:self
options:nil];
[self viewDidLoad];
}
}
}
// pre-iOS6 rotating options
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
// iOS6 rotating options
- (BOOL)shouldAutorotate {
return YES;
}
// iOS6 interface orientations
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
- (void)viewDidLoad {
[super viewDidLoad];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[start addGestureRecognizer:longPress];
recordLength = 10;
countdown = 10;
useDev = TRUE;
self.navigationItem.rightBarButtonItem = menuButton;
iapi = [iSENSE getInstance];
[iapi toggleUseDev: useDev];
running = NO;
timeOver = NO;
setupDone = NO;
if (useDev) {
expNum = DEV_DEFAULT_EXP;
} else {
expNum = PROD_DEFAULT_EXP;
}
dfm = [[DataFieldManager alloc] init];
//[dfm setEnabledField:YES atIndex:fACCEL_Y];
motionmanager = [[CMMotionManager alloc] init];
userName = @"sor";
passWord = @"sor";
firstName = @"No Name";
lastInitial = @"Provided";
[self login:@"sor" withPassword:@"sor"];
if (managedObjectContext == nil) {
managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
// DataSaver from Data_CollectorAppDelegate
if (dataSaver == nil) {
dataSaver = [(AppDelegate *) [[UIApplication sharedApplication] delegate] dataSaver];
NSLog(@"Datasaver Details: %@", dataSaver.description);
NSLog(@"Current count = %d", dataSaver.count);
}
}
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (self.isMovingToParentViewController == YES) {
change_name = [[CODialog alloc] initWithWindow:self.view.window];
change_name.title = @"Enter First Name and Last Initial";
[change_name addTextFieldWithPlaceholder:@"First Name" secure:NO];
[change_name addTextFieldWithPlaceholder:@"Last Initial" secure:NO];
UITextField *last = [change_name textFieldAtIndex:1];
[last setDelegate:self];
[change_name addButtonWithTitle:@"Done" target:self selector:@selector(changeName)];
[change_name showOrUpdateAnimated:YES];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
BOOL x1 = [prefs boolForKey:@"X"];
BOOL y1 = [prefs boolForKey:@"Y"];
BOOL z1 = [prefs boolForKey:@"Z"];
BOOL mag1 = [prefs boolForKey:@"Magnitude"];
if (x)
x = x1;
if (y)
y = y1;
if (z)
z = z1;
if (mag)
mag = mag1;
[self willRotateToInterfaceOrientation:(self.interfaceOrientation) duration:0];
NSLog(@"Frog FROG FROG");
/*[image setAutoresizesSubviews:YES];
image.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
*/
}
}
AppDelegate.m から:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
} else {
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
}
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:self.viewController];
navigation.navigationBar.barStyle = UIBarStyleBlackOpaque;
self.window.rootViewController = navigation;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setBool:YES forKey:@"Y"];
self.viewController.setupDone = YES;
[self.window makeKeyAndVisible];
return YES;
}