1

我的 loadView 方法中有这个条件浮点数,它适用于两个设备。然而,我在试图找到一种方法来改变这些方向时遇到了最糟糕的情况。

我的 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);

那里有很多解决方案,但我不是那么敏锐。

=====

Matt Neuburg 指导我考虑 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];

这是结果:

在此处输入图像描述

在此处输入图像描述

4

3 回答 3

1

正如其他人所暗示的那样,问题在于这viewDidLoad还为时过早。我在书中给出了一堆解决方案:

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

viewDidLayoutSubviews是一个很棒的地方。iOS 6 中最棒的就是给所有的东西提供良好的约束,然后你就不需要任何关于轮换的代码了。

于 2013-04-06T00:30:03.417 回答
0

老实说,我真的不知道你的目标是什么,但我很确定如果你把这段代码移到

- (void)viewDidLayoutSubviews

方法,您的问题将得到解决!

于 2013-04-05T21:29:03.757 回答
0

您是否尝试过使用这种方法?

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    // Your code here
}
于 2013-04-06T00:23:45.657 回答