2013-04-05 67 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 
}