2013-11-25 31 views
1

我在带有UIImageView和UILabel的Tab Bar Controller中有一个视图。我定位了UILabel,这样文本就会出现在UIImageView的心脏中。图像视图占据了整个视图。我使用AutoLayout来定位所有内容,并设置为旋转时将图像拉伸以填充整个屏幕,但我遇到了将UILabel放在需要的位置的问题。AutoLayout不能在UILabel上工作

我试着将顶部粘贴到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

回答

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; 
} 
+0

我试过这个,但在'self.view addConstraint'我没有找到topConstraintForView的类方法。 – user717452

+0

@ user717452,你将不得不按照我的方式添加类别。我已经更新了我的答案,以向您展示如何在不添加类别的情况下执行此操作。 – rdelmar

+0

太棒了!谢谢你! – user717452

0

已经在这里无尽的时间在你的循环需要我会建议使用代码来定位标签方法。创建一个IBOutlet,然后在设备旋转时将框架调整到两个位置之一。

+0

试过这种使用willRotateToInterfaceOrientation,并设置为的UILabel对于每个方向的框架,但它在所有没有进行任何更改。我为每个方向添加了NSLogs,并且在旋转时适当地点燃,但框架不起作用。 – user717452

+0

你连接了IBOutlet吗? – GuybrushThreepwood

+0

是的。刚刚确认它是。 – user717452

0

更有可能的是,你将不得不使文本变得更小,以便动态地适应心脏。同样,如果心不是自己的观点,那就把它当成自己的观点吧。这将有助于自动布局知道如何更好地定义自己。此外,为了帮助可能得到更多的理解,这是一个很好的资源http://nsscreencast.com/episodes/35-autolayout-fun

祝你好运。

相关问题