2013-03-21 29 views
1

我已經創建了兩個.xib文件之一肖像模式和其他的景觀模式,加載不同的廈門國際銀行文件

每個旋轉我要加載相應的.xib文件,

這裏是我的代碼片段,

ViewAppDelegate.m類

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

    if ([UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortrait | [UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortraitUpsideDown) 
    { 
     self.viewController = [[OrientationViewController alloc] initWithNibName:@"PortraitController" bundle:nil]; 
     UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:self.viewController]; 
     self.window.rootViewController = navigationController; 
    } 
    else{ 
     self.viewController = [[OrientationViewController alloc] initWithNibName:@"LandscapeController" bundle:nil]; 
     UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:self.viewController]; 
     self.window.rootViewController = navigationController; 
    } 

    [self.window makeKeyAndVisible]; 
    return YES; 
} 

ViewController.m類

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{ 
    return YES; 
} 


-(void)viewWillLayoutSubviews{ 

    if ([UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortrait | [UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortraitUpsideDown) 
    { 
     [[NSBundle mainBundle] loadNibNamed:@"PortraitController" owner:self options:nil]; 
    } 
    else{ 
     [[NSBundle mainBundle] loadNibNamed:@"LandscapeController" owner:self options:nil]; 
    } 

} 

寫這麼多的代碼我的應用程序後顯示什麼..它只顯示黑屏。

請提出一些解決方案。

+0

什麼發生在viewWillLayoutSubviews中保存在shouldAutorotateToInterfaceOrientation中的代碼? – Balu 2013-03-21 06:26:25

+0

它不顯示黑屏,它加載肖像模式的xib文件,當我將它旋轉到風景時,它不會加載風景的xib文件。 – Krunal 2013-03-21 06:30:41

+0

您是否連接了LandscapeController的文件所有者? – Balu 2013-03-21 06:32:30

回答

2

一次嘗試這樣

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeOrientation) name:UIDeviceOrientationDidChangeNotification object:nil]; 

添加這個方法到應用程序視圖控制器來獲得通知。

-(void)changeOrientation { 
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation; 
    if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) { 
     [[NSBundle mainBundle] loadNibNamed:@"Portrait" owner:self options:nil]; 
    } 
    else { 
     [[NSBundle mainBundle] loadNibNamed:@"Landscape" owner:self options:nil]; 
    } 

} 
+0

不錯,它工作..但是當我啓動我的應用程序在肖像模式下,它加載景觀不是肖像的xib文件..我寫了第一個兩行,你建議'viewWillAppear'方法 – Krunal 2013-03-21 06:54:02

0
-(void)setFramesForPotrait 
{ 

// set frames here 

} 

-(void)setFramesForLandscaeMode 
{ 

// set frames here 

} 

-(bool)shouldAutorotate..... 
{ 
return Yes 
} 

-()willAutoRotate...... 
{ 
if(orientation = uiinterfaceOrientationPotrait) 
{ 
[self setFramesForPotrait]; 
} 
else 
{ 
[self setFramesForLandscape]; 
} 

爲什麼不u盤幀進行橫向和縱向的單一視圖控制器

+0

我可以使用框架,但使用xib我可以快速設計我的界面.. – Krunal 2013-03-21 06:35:52

+0

你做你用xib,但然後在代碼中把你的框架景觀和肖像 – Kasaname 2013-03-21 06:37:13

2

可以使用willRotateToInterfaceOrientation在viewController.m實現這一目標

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) 
    { 
     [[NSBundle mainBundle] loadNibNamed: [NSString stringWithFormat:@"%@-landscape", NSStringFromClass([self class])] 
             owner: self 
            options: nil]; 
     [self viewDidLoad]; 
    } 
    else 
    { 
     [[NSBundle mainBundle] loadNibNamed: [NSString stringWithFormat:@"%@", NSStringFromClass([self class])] 
             owner: self 
            options: nil]; 
     [self viewDidLoad]; 
    } 
} 

,你可以檢查你的viewwillAppear方法定位像下面分別負載xibs:

-(void)viewWillAppear:(BOOL)animated{ 
    [super viewWillAppear:YES]; 

    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){ 
     [[NSBundle mainBundle] loadNibNamed: [NSString stringWithFormat:@"%@", NSStringFromClass([self class])] 
             owner: self 
            options: nil]; 
     [self viewDidLoad]; 
    } 
    else 
    { 
     [[NSBundle mainBundle] loadNibNamed: [NSString stringWithFormat:@"%@-landscape", NSStringFromClass([self class])] 
             owner: self 
            options: nil]; 
     [self viewDidLoad]; 
    } 
} 

這裏,我有名稱xibs,如
肖像:viewControllerName
Land scape:viewControllerName-landscape

我再次調用viewDidload方法來重新載入新的xib。

另一種實現方法是: GPOrientationKit
這也是一個不錯的選擇!

或者,

你可以通過創建2個xibs具有相同的標籤名稱,使一個LayoutManager class的標籤映射並獲取幀,並分配相應的橫向/縱向框架所有控制旋轉創建控件變化框架(通過從xibs中獲取)。

+0

它正在工作..但是當我在肖像模式啓動我的應用程序時,它加載景觀的xib文件而不是肖像。 – Krunal 2013-03-21 07:19:02

+0

好的,我正在編輯我的回答 – 2013-03-21 07:29:09

+0

@Krunal請檢查編輯。 – 2013-03-21 07:37:28

相關問題