私はユニバーサルアプリを持っています。縦向きビューと横向きビューに別々の xib を使用しています。BOOL
横向きの場合、アプリで向きを検出し、 a の値を true に変更します。それが本当の場合、ランドスケープxibをロードする方法を知りたいBOOL
です。これを達成するためにいくつかの異なる方法を試しましたが、何もうまくいきませんでした。この件に関するご意見をお待ちしております。この投稿を更新して、必要なコード スニペットを含めることができます。前もって感謝します。
編集:これらすべてを 1 つの ViewController クラスで行いたいのですが、iPhone ではなく、iPad に対してのみ行います。私はその部分をすべて解決しました。ランドスケープxibをロードするだけです。
編集:私viewDidLoad
はこれをやっています:
if (userDevice.orientation == UIDeviceOrientationLandscapeLeft || userDevice.orientation == UIDeviceOrientationLandscapeRight) {
landscape = YES;
}
これが私のメインのView Controller .mです:
@implementation PassportAmericaViewController
@synthesize browseViewButton, webView, mainView, lblMemberName, menuOpen, internetActive, hostActive, isUsingiPad, portrait, landscape;
- (void)viewDidLoad {
menuOpen = NO;
UIDevice* userDevice = [UIDevice currentDevice];
if (userDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
isUsingiPad = YES;
}
if (isUsingiPad)
if (userDevice.orientation == UIDeviceOrientationLandscapeLeft || userDevice.orientation == UIDeviceOrientationLandscapeRight) {
landscape = YES;
}
[self checkForKey];
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES];
}
-(void)viewWillAppear:(BOOL)animated{
[self.navigationController setNavigationBarHidden:YES];
}
-(void) checkForKey{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
int regCheck = [defaults integerForKey:@"registration"];
if (isUsingiPad) {
if (regCheck == 0) {
RegistrationViewController *regView = [[RegistrationViewController alloc]
initWithNibName:@"RegistrationView-iPad" bundle:[NSBundle mainBundle]];
regView.isUsingiPad = YES;
[self.navigationController pushViewController:regView animated:YES];
}else if (regCheck == 1) {
@try {
NSString *mbrFirstName = [defaults objectForKey:@"firstName"];
NSString *mbrLastName = [defaults objectForKey:@"lastName"];
NSMutableString *name = [[NSMutableString alloc] initWithString:mbrFirstName];
[name appendString:@" "];
[name appendString:mbrLastName];
lblMemberName.text = name;
}
@catch (NSException *exception) {
}
}
}else{
if (regCheck == 0) {
RegistrationViewController *regView = [[RegistrationViewController alloc]
initWithNibName:@"RegistrationView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:regView animated:YES];
}else if (regCheck == 1) {
@try {
NSString *mbrFirstName = [defaults objectForKey:@"firstName"];
NSString *mbrLastName = [defaults objectForKey:@"lastName"];
NSMutableString *name = [[NSMutableString alloc] initWithString:mbrFirstName];
[name appendString:@" "];
[name appendString:mbrLastName];
lblMemberName.text = name;
}
@catch (NSException *exception) {
}
}
}
}
-(IBAction) openBrowseView{
if (isUsingiPad && landscape) {
BrowseViewController *browseView = [[BrowseViewController alloc]
initWithNibName:@"BrowseView-iPadLandscape" bundle:[NSBundle mainBundle]];
browseView.isUsingiPad = YES;
[self.navigationController pushViewController:browseView animated:YES];
}else if (isUsingiPad){
BrowseViewController *browseView = [[BrowseViewController alloc]
initWithNibName:@"BrowseView-iPad" bundle:[NSBundle mainBundle]];
browseView.isUsingiPad = YES;
[self.navigationController pushViewController:browseView animated:YES];
}else{
BrowseViewController *browseView = [[BrowseViewController alloc]
initWithNibName:@"BrowseView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:browseView animated:YES];
}
}
-(IBAction) openViewMore{
if (isUsingiPad) {
ViewMoreViewController *viewMoreView = [[ViewMoreViewController alloc]
initWithNibName:@"ViewMoreView-iPad" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:viewMoreView animated:YES];
viewMoreView.isUsingiPad = YES;
}else{
ViewMoreViewController *viewMoreView = [[ViewMoreViewController alloc]
initWithNibName:@"ViewMoreView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:viewMoreView animated:YES];
}
}
-(IBAction) callTollFree:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:8002837183"]];
}
-(IBAction)clickToJoin:(id)sender {
if (isUsingiPad) {
webView = [[WebViewController alloc]
initWithNibName:@"WebView-iPad" bundle:[NSBundle mainBundle]];
webView.url=@"http://www.passport-america.com/Members/JoinRenew.aspx";
[self.navigationController pushViewController:webView animated:YES];
webView.isUsingiPad = YES;
}else {
webView = [[WebViewController alloc]
initWithNibName:@"WebView" bundle:[NSBundle mainBundle]];
webView.url=@"http://www.passport-america.com/Members/JoinRenew.aspx";
[self.navigationController pushViewController:webView animated:YES];
}
}
-(IBAction) iPadContactUs:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"mailto:info@passport-america.com"]];
}
-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
if ([animationID isEqualToString:@"slideMenu"]){
UIView *sq = (__bridge UIView *) context;
[sq removeFromSuperview];
}
}
-(void) positionViews {
if (isUsingiPad) {
UIInterfaceOrientation destOrientation = self.interfaceOrientation;
if (destOrientation == UIInterfaceOrientationPortrait || destOrientation == UIInterfaceOrientationPortraitUpsideDown) {
PassportAmericaViewController *homeView2 = [[PassportAmericaViewController alloc]
initWithNibName:@"PassportAmericaViewController-iPad" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:homeView2 animated:YES];
homeView2.isUsingiPad = YES;
homeView2.portrait = YES;
homeView2.landscape = NO;
}else{
PassportAmericaViewController *homeView2 = [[PassportAmericaViewController alloc]
initWithNibName:@"PassportAmericaViewController-iPadLandscape" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:homeView2 animated:YES];
homeView2.isUsingiPad = YES;
homeView2.portrait = NO;
homeView2.landscape = YES;
}
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (isUsingiPad) {
return YES;
}else{
// Return YES for supported orientations
return NO;
}
}
-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration: (NSTimeInterval)duration {
if (isUsingiPad) {
[self positionViews];
}else{
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
@end