gpt4 book ai didi

ios - AutoLayout 不适用于 UILabel

转载 作者:行者123 更新时间:2023-11-29 10:51:32 25 4
gpt4 key购买 nike

我在带有 UIImageView 和 UILabel 的选项卡栏 Controller 中有一个 View 。我放置了 UILabel,因此文本将出现在 UIImageView 的心形内部。 ImageView 占据了整个 View 。我正在使用 AutoLayout 来定位所有内容,并将其设置为在旋转时拉伸(stretch)图像以填满整个屏幕,但是我在将 UILabel 放在我想要的位置时遇到了问题。

我已经尝试将顶部固定到 SuperView,但这会导致 UILabel 太低,并且固定到底部会在旋转时将 UILabel 推出 View 。这是显示第一件事的图像。

更新:我尝试了以下代码,但无济于事:

-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
NSLog(@"Landscape left");
label1.frame = CGRectMake(29, 20, 509, 142);
[label1 setFrame:CGRectMake(29, 20, 509, 142)];

} else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
NSLog(@"Landscape right");
label1.frame = CGRectMake(29, 20, 509, 142);
[label1 setFrame:CGRectMake(29, 20, 509, 142)];

} else if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
NSLog(@"Portrait");
[label1 setFrame:CGRectMake(29, 77, 61, 142)];
}
}

enter image description here enter image description here

最佳答案

在使用自动布局时,切勿直接设置框架。所有移动和调整大小都应通过约束来完成。您可以使用乘数和常量在代码中设置标签的位置,这样 View 将自动更改其位置而无需检查方向。例如,如果你有这个,

NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:subview attribute:NSLayoutAttributeTop relatedBy:0 toItem:self.view attribute:NSLayoutAttributeBottom multiplier:0.2 constant:-60];

subview 在竖屏模式下距顶部 53.6 点,在横屏模式下距顶部 4 点。进行计算以找出要插入的数字有点痛苦,所以我在 NSLayoutConstraint 上写了一个类别来为我做这件事。所以类别有一个方法,看起来像这样:

+(NSLayoutConstraint *)topConstraintForView:(UIView *)subview viewAttribute:(NSLayoutAttribute) att superview:(UIView *)superview portraitValue:(CGFloat)pValue landscapeValue:(CGFloat)lValue {
CGFloat multiplier = (pValue - lValue)/(superview.bounds.size.height - superview.bounds.size.width);
CGFloat constant = pValue - (superview.bounds.size.height * multiplier);
NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:subview attribute:att relatedBy:0 toItem:superview attribute:NSLayoutAttributeBottom multiplier:multiplier constant:constant];
return con;
}

你可以很简单地使用它,就像这样:

[self.view addConstraint:[NSLayoutConstraint topConstraintForView:self.label viewAttribute:NSLayoutAttributeTop superview:self.view portraitValue:100 landscapeValue:50]];

这将计算乘数和常量的正确值,并正确定位 View 。在 IB 中,您需要对标签设置一个顶部约束,在添加该标签之前将其删除。您可以通过编辑约束并选中标题为“Placeholder Remove at build time”的框来做到这一点。

如果您想在不使用类别的情况下执行此操作,您可以稍微更改方法,并将其添加到要设置约束的类中。像这样:

- (void)viewDidLoad {
[super viewDidLoad];
[self.view addConstraint:[self topConstraintForView:self.label viewAttribute:NSLayoutAttributeTop superview:self.view portraitValue:100 landscapeValue:50]];
}



-(NSLayoutConstraint *)topConstraintForView:(UIView *)subview viewAttribute:(NSLayoutAttribute) att superview:(UIView *)superview portraitValue:(CGFloat)pValue landscapeValue:(CGFloat)lValue {
CGFloat multiplier = (pValue - lValue)/(superview.bounds.size.height - superview.bounds.size.width);
CGFloat constant = pValue - (superview.bounds.size.height * multiplier);
NSLayoutConstraint *con = [NSLayoutConstraint constraintWithItem:subview attribute:att relatedBy:0 toItem:superview attribute:NSLayoutAttributeBottom multiplier:multiplier constant:constant];
return con;
}

关于ios - AutoLayout 不适用于 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20196873/

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