我的 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];
这是结果: