gpt4 book ai didi

iOS 改变方向的高度

转载 作者:行者123 更新时间:2023-11-29 13:15:53 24 4
gpt4 key购买 nike

我在我的 loadView 方法中有这个条件 float ,它适用于两个设备。然而,我在尝试找到一种方法来改变方向时遇到了最糟糕的情况。

我的 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];

这是结果:

enter image description here

enter image description here

最佳答案

正如其他人所暗示的,问题是 viewDidLoad 太早了。我在我的书中给出了一堆解决方案:

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

viewDidLayoutSubviews 是一个极好的地方。 iOS 6 中最好的一点就是给所有的东西都很好的约束,然后你根本不需要任何关于旋转的代码。

关于iOS 改变方向的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15843734/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com