AutoLayout不适用于UILabel

时间:2013-11-25 15:30:53

标签: ios autolayout

我在Tab Bar Controller中有一个带有UIImageView和UILabel的视图。我将UILabel定位,因此文本将出现在UIImageView的心脏内部。图像视图占据整个视图。我正在使用AutoLayout定位所有内容,并将其设置为在旋转时拉伸图像以填充整个屏幕,但是我遇到了将UILabel放到我想要的位置的问题。

我已经尝试将Top To固定为SuperView,但这导致UILabel太低,并且在旋转时固定到底部将UILabel推出视野。这是首先显示的图像。

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

-(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

4 个答案:

答案 0 :(得分:2)

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

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

子视图位于纵向顶部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]];

这将计算乘数和常数的正确值,并正确定位视图。在IB中,您需要对标签有一个顶级约束,在添加此标签之前将其删除。您可以通过编辑约束并选中标题为“在构建时删除占位符”的框来执行此操作。

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

- (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;
}

答案 1 :(得分:0)

在这里无休止的时候,我建议使用代码来根据旋转方法的需要定位标签。创建IBOutlet,然后在设备旋转时将框架调整为两个位置之一。

答案 2 :(得分:0)

更可能的是,您必须使文本更小,以便动态地适应心脏。如果心脏不是自己的观点,那就把它做成自己的。这将有助于自动布局以了解如何更好地定义自己。另外,为了帮助我们获得更多的理解,这是一个很好的资源http://nsscreencast.com/episodes/35-autolayout-fun

祝你好运。

答案 3 :(得分:0)

简单一步, 只需使用与您的需求相关的约束即可。

enter image description here

然后根据您的需要设置关系> =或< =或== 。 还要在界面构建器中设置行数。

enter image description here

参考:https://medium.com/@elgoni/ios-7-auto-layout-3e50cd1d1278

相关问题