我的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];
这就是结果:
答案 0 :(得分:1)
正如其他人暗示的那样,问题是viewDidLoad
太早了。我在书中提出了一堆解决方案:
http://www.apeth.com/iOSBook/ch19.html#SECrotationevents
viewDidLayoutSubviews
是一个很棒的地方。最重要的是在iOS 6中只是给出了所有好的约束条件,然后根本不需要任何旋转代码。
答案 1 :(得分:0)
老实说,我真的不知道你的目标是什么,但我很确定如果你将这段代码移到
- (void)viewDidLayoutSubviews
方法,你的问题将得到解决!
答案 2 :(得分:0)
您是否尝试过使用此方法?
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
// Your code here
}