2013-04-05 6 views
1

2 개의 장치에서 잘 작동하는 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); 

있습니다 거기 많은 솔루션은,하지만 난 그렇게 선명하지 않다.

=====

매트 노이 부르크 나를 안내

[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]; 

그리고이 결과 : NSLayoutConstraint을 고려 ... 이것은 내가 소집 무엇

enter image description here

솔직히

enter image description here

답변

1

, 문제는 viewDidLoad가 너무 빨리 것입니다. 내 책에 솔루션 무리를 줄 :

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

viewDidLayoutSubviews 훌륭한 장소입니다. iOS 6의 모든 기능 중 가장 좋은 점은 모든 제약 조건을 만족시키는 것입니다. 그러면 순환 게재에 대한 코드가 전혀 필요하지 않습니다.

+0

매트! 제약이 핵심이었다. – Morkrom

0

내가 그나마 정말 당신의 목표는 무엇인지 알고 있지만, 이 코드를이 코드로 이동하면

- (void)viewDidLayoutSubviews 

방법을 사용하면 문제가 해결 될 것입니다!

0

이 방법을 사용해 보았습니까? 다른 사람이 암시 한 것처럼

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