2014-12-19 28 views
1

我不得不問這個問題,因爲我完全被這個機制所困惑。我試圖檢查縱向方向,雖然它在我的情況下可以在viewdidload中正常工作,但因爲我只需要打一次電話,所以我仍然想知道如果每次設備返回時都需要檢查肖像,該怎麼辦肖像。viewDidLayoutSubviews:和UIInterfaceOrientationIsPortrait

我有以下代碼

-(void)viewDidLayoutSubviews{ 
if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){ 
    self.adviceLabel.preferredMaxLayoutWidth = [[UIScreen mainScreen]bounds].size.width-25; 
    [self.adviceLabel setFrame:CGRectMake(0, 0,[[UIScreen mainScreen]bounds].size.width-25 , 103)]; 
    NSLog(@"portrait"); 
} 

首先「肖像」的被愛過的3倍,我想是因爲我有我的VC裏面3次,咄。但是當我開始轉動屏幕時,實際上我開始一直記錄「肖像」,當我「轉向風景」時意味着它在我打開風景模式後記錄下來。這背後有什麼意思嗎?我討厭留下一個不明白的話題。提前致謝!以下爲你的目標代碼

回答

0

用途:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
// do something before rotation 
} 

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
// do something after rotation 
} 

-viewDidLayoutSubviews方法被調用每當視圖和它的子視圖改變它們的幀。每次視圖改變其框架時,檢查方向並不是一個好習慣。

+0

你也可以添加方向標誌來檢查它在任何你想要的。 – 2014-12-19 14:08:28

+0

你的定位標誌是什麼意思?關於你的答案,我不能使用這些方法,因爲我的確在尋找使用UIInterfaceOrientationIsPortrait,但我不明白爲什麼它在viewdidlayoutsubjects旋轉到橫向時返回true。 (在其他方法,如viewdidload它按預期工作..) – DevilInDisguise 2014-12-19 14:29:29

+0

您可以添加布爾屬性爲例。 @property(nonatomic,assign)BOOL viewOrientation。並在上面的方法中進行更改。 – 2014-12-19 14:47:02

相關問題