不能移动的UILabel与自动布局&QUOT Y位置;在"鉴于 [英] Can't move UILabel's Y position with Autolayout "on" in view

查看:157
本文介绍了不能移动的UILabel与自动布局&QUOT Y位置;在"鉴于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须失去了一些东西非常明显的位置,但这已经是一直折腾我好几天的问题。

I must be missing something terribly obvious here, but this has been an issue that's been frustrating me for DAYS.

在对x code ++ 4.5在iOS项目,我在的UIScrollView 占据了在厦门国际银行多个标签,一个在另一个之上,一个的UIView 。每个标签是作为视图宽,且各自为上面的下一个约20像素。有时,一个标签没有任何信息,因此它被设置为不可见,并在它下面的标签都应该动起来占据空白。

In an iOS project on xcode 4.5, I've got several labels in a XIB, one on top of the other, in a UIScrollView that occupies a UIView. Each label is as wide as the view, and each is about 20 px above the next. On occasion, one of the labels doesn't have any information, so it gets set to invisible, and the labels below it are supposed to move up to occupy the blank space.

问题是,如果自动布局被选中关闭的观点,正好标签向上,因为他们应该,虽然的UIScrollView 不再滚动。如果是在 ,标签不动弹,不管是什么。

The problem is, if autolayout is checked "off" on the view, the labels move up exactly as they should, though the UIScrollView no longer scrolls. If it is on, the labels do not move at all, no matter what.

下面是code ......我基本上只用一个快捷功能的无形标签的高度移动的每个标签了。

Here is the code... I basically just use a quick function to move each label up by the height of the invisible label.

[self moveObjectBy: self.festNameLabel moveByY:-(yearsLabel.frame.size.height-2)];


// this just quickly moves a label. 
- (void)moveObjectBy:(UIView *)lbl moveByY:(int)byHeight {
    CGRect newFrame = lbl.frame;
    NSLog(@"%f, %d", newFrame.origin.y, byHeight);
    newFrame.origin.y += byHeight; //yearsLabel.frame.size.height;
    lbl.frame = newFrame;
}

当它运行时,的NSLog 显示它的Y已移动,但它不会在屏幕上移动。我敢肯定,它与垂直空间的限制做的,但它不会让我删除的约束,它不会让我将其更改为比从视图的顶部空间的其它任何东西。就像我说的,我敢肯定,我失去了一些东西,但我已经筋疲力尽,我知道做的一切......

When it's run, the NSLog shows it's Y has moved, but it doesn't move on the screen. I'm sure it has to do with the vertical space constraint, but it won't let me delete the constraint, and it won't let me change it to anything other than space from the top of the view. Like I said, I'm sure I'm missing something, but I've exhausted everything I know to do...

推荐答案

如果你只有一个标签可能被隐藏,你可以用下面的移动它下面的人,起来:

If you have just one label that might be hidden, you can move the ones below it, up with the following:

 -(void)contract {
    self.label2.hidden = YES;
    self.con2To1.constant = -34;
}

顶部标签具有约束到滚动视图的顶部,和所有其他的标签有20个点的垂直距离限制到上面和下面他们的人。在这个例子中,我躲在LABEL2。这些标签都是34分高,所以从20到-34举动现在隐藏标签改变约束权恒在它上面的一个顶部。

The top label has a constraint to the top of the scroll view, and all the other labels have 20 point vertical distance constraints to the ones above and below them. In this example, I'm hiding label2. The labels are all 34 points high, so changing the constraint constant from 20 to -34 moves the now hidden label right on top of the one above it.

要使用此方法与多个标签可以被隐藏,你需要有一个出口到每个要隐藏的标签,并对其约束上面的标签。实际上,你可以不用网点的制约,但我不知道它是否会像健壮的(这是可能的,它可能会选错约束)。我能够通过限制循环地发现,具有特定标签而来的一个做同样的事情,而且是一到该标签的顶部:

To use this method with multiple labels that can be hidden, you will need to have an outlet to each of the labels you want to hide, and to its constraint to the label above. You actually can do it without outlets to the constraints, but I don't know if it would be as robust (it's possible that it might pick the wrong constraint). I was able to do the same thing by looping through the constraints to find the one that goes with a particular label, and is the one to the top of that label:

-(void)hideLabel:(UILabel *) label {
    label.hidden = YES;
    for (NSLayoutConstraint *con in self.scroller.constraints) {
        if (con.firstItem == label  && con.firstAttribute == NSLayoutAttributeTop) {
            con.constant = -34;
        }
    }
}

同样的方法可以修改,如果你喜欢用动画 - 下面的代码片段淡出您想在动画的拉升所有较低的标签隐藏标签

The same method can be modified to use an animation if you like -- the following snippet fades out the label you want to hide while animating the move up of all the lower labels.

 -(void)hideLabel:(UILabel *) label {
    for (NSLayoutConstraint *con in self.scroller.constraints) {
        if (con.firstItem == label  && con.firstAttribute == NSLayoutAttributeTop) {
            con.constant = -34;
            [UIView animateWithDuration:.5 animations:^{
                label.alpha = 0;
                [self.scroller layoutIfNeeded];
            }];
        }
    }
}

这篇关于不能移动的UILabel与自动布局&QUOT Y位置;在"鉴于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆