2013-04-05 73 views
1

我在我的loadView方法中有這個條件浮點數,這對兩個設備來說工作正常。不過,我最糟糕的時間是想找到一種方法來改變方向。iOS改變方向的高度

我有這個在我的Prefix.pch:

#define dDeviceOrientation [[UIDevice currentDevice] orientation] 
#define isPortrait UIDeviceOrientationIsPortrait(dDeviceOrientation) 
#define isLandscape UIDeviceOrientationIsLandscape(dDeviceOrientation) 
#define isFaceUp dDeviceOrientation == UIDeviceOrientationFaceUp ? YES : NO 
#define isFaceDown dDeviceOrientation == UIDeviceOrientationFaceDown ? YES : NO 

我有這個在我的loadView方法(肖像,因爲我想知道,工作第一...:

CGRect screenBounds = [[UIScreen mainScreen] bounds]; 
CGFloat screenHeight = CGRectGetHeight(screenBounds);  
    float Y; 
if ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)){   
    NSLog(@"%f", screenHeight); 
    if (isPortrait){ 
Y = (screenHeight-(0.14*screenHeight)); 
    }else{ 
    } 
} else { 
    if (isPortrait){ 
     Y = (screenHeight-(0.25*screenHeight)); 
    }else{    
    } 
} 
settingsButton.frame = CGRectMake(20, Y, 40, 40.0); 
aboutButton.frame = CGRectMake(75, Y, 40, 40.0); 

有許多解決方案在那裏,但我不那麼尖銳。

=====

馬特·諾伊堡引導我考慮NSLayoutConstraint ...這是我召集:

[self.view addSubview:ab]; 
[self.view addSubview:sb]; 
//constraints 
NSLayoutConstraint *constraint =[NSLayoutConstraint 
            constraintWithItem:ab 
           //ab's relation to the left edge 
            attribute:NSLayoutAttributeLeading          
            relatedBy:NSLayoutRelationEqual 
            toItem:self.view 
            attribute:NSLayoutAttributeLeading          
            multiplier:1.0f 
            constant:7.f]; 
[self.view addConstraint:constraint]; 

constraint = [NSLayoutConstraint 
       constraintWithItem:ab 
       //ab's relation to the bottom edge 
       attribute:NSLayoutAttributeBottom 
       relatedBy:NSLayoutRelationEqual 
       toItem:self.view 
       attribute:NSLayoutAttributeBottom 
       multiplier:1.0f 
       constant:-10.f]; 

[self.view addConstraint:constraint]; 

constraint = [NSLayoutConstraint constraintWithItem:sb 
      //sb's identical relation to the bottom edge (violation of DRY but I'm in a hurry :'(// 
       attribute:NSLayoutAttributeBottom 
       relatedBy:NSLayoutRelationEqual 
       toItem:self.view attribute:NSLayoutAttributeBottom 
       multiplier:1.0f constant:-10.f]; 
[self.view addConstraint:constraint]; 

constraint = [NSLayoutConstraint 
       //SB's relation to the leading edge 
       constraintWithItem:sb 
       attribute:NSLayoutAttributeLeading 
       relatedBy:NSLayoutRelationEqual 
       toItem:self.view 
       attribute:NSLayoutAttributeLeading 
       multiplier:1.0f 
       constant:75.0f]; 
[self.view addConstraint:constraint]; 

這是結果:

enter image description here

enter image description here

回答

1

正如其他人暗示的那樣,問題在於viewDidLoad太快了。我給了一堆的解決方案在我的書:

http://www.apeth.com/iOSBook/ch19.html#SECrotationevents

viewDidLayoutSubviews是一個很好的地方。在iOS 6中最好的就是給所有的東西提供很好的約束,然後你根本不需要任何代碼。

+0

謝謝你馬特!約束是關鍵。 – Morkrom 2013-04-08 17:18:35

0

老實說,我真的不知道自己的目標是什麼,但我很確定,如果您將此代碼移動到

- (void)viewDidLayoutSubviews 

方法,你的問題將被修復!

0

您是否嘗試過使用此方法?

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    // Your code here 
}