锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

当前位置:锐英源 / 开源技术 / ios开源技术 / OC旋转和改变方向常用方法
服务方向
人工智能数据处理
人工智能培训
kaldi数据准备
小语种语音识别
语音识别标注
语音识别系统
语音识别转文字
kaldi开发技术服务
软件开发
运动控制卡上位机
机械加工软件
软件开发培训
Java 安卓移动开发
VC++
C#软件
汇编和破解
驱动开发
联系方式
固话:0371-63888850
手机:138-0381-0136
Q Q:396806883
微信:ryysoft

锐英源精品开源,禁止转载和任何形式的非法内容使用,违者必究


OC旋转和改变方向常用方法

I've got this conditional float in my loadView method, which works fine for the two devices. However, I've had the worst time trying to find a way to make these change for orientation.

I have this in my Prefix.pch:我在我的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  

I have this in my loadView method (portrait because I want to know that works first...:我在我的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);  

There are many solutions out there, but I am not so sharp.有很多解决方案,但我不是那么热切。

=====

Matt Neuburg guided me to consider NSLayoutConstraint... This is what I mustered: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];

 

As others have implied, the problem is that viewDidLoad is way too soon. I give a bunch of solutions in my book:正如其他人暗示的那样,问题是这viewDidLoad太快了。我在书中给出了一堆解决方案:

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

viewDidLayoutSubviews is an excellent place. Best of all in iOS 6 is just give everything good constraints and then you don't need any code on rotation at all.viewDidLayoutSubviews是一个很棒的地方。最重要的是在iOS 6中只是给出了一切好的约束,然后根本不需要任何旋转代码。

Honestly I dont really know what your goal is, but I'm quite sure that if you move this code to the老实说,我真的不知道你的目标是什么,但我很确定如果你把这个代码移到了

- (void)viewDidLayoutSubviews  

method, your problem will be fixed!方法,你的问题将得到解决!

Have you tried using this method?你尝试过使用这种方法吗?

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration  {      // Your code here  }
友情链接
版权所有 Copyright(c)2004-2021 锐英源软件
公司注册号:410105000449586 豫ICP备08007559号 最佳分辨率 1024*768
地址:郑州大学北校区院(文化路97号院)内