【问题标题】:iOS change height for orientationiOS改变方向的高度
【发布时间】:2013-04-05 21:17:10
【问题描述】:

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

结果如下:

【问题讨论】:

    标签: ios objective-c screen orientation c-preprocessor


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢马特!约束是关键。
    【解决方案2】:

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

    - (void)viewDidLayoutSubviews
    

    方法,你的问题会解决的!

    【讨论】:

      【解决方案3】:

      你试过用这种方法吗?

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

      【讨论】:

        猜你喜欢
        • 2012-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多