精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
锐英源精品开源,禁止转载和任何形式的非法内容使用,违者必究
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 }