我一直在为 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;
}