我在我的loadView方法中有这个条件浮点数,这对两个设备来说工作正常。不过,我最糟糕的时间是想找到一种方法来改变方向。iOS改变方向的高度
我有这个在我的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);
有许多解决方案在那里,但我不那么尖锐。
=====
马特·诺伊堡引导我考虑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];
这是结果:
谢谢你马特!约束是关键。 – Morkrom 2013-04-08 17:18:35