【问题标题】:Hide and show UIView with bottom View filling the space when hidden using autolayout使用自动布局隐藏时隐藏和显示 UIView,底部视图填充空间
【发布时间】:2014-04-14 06:27:28
【问题描述】:

我有两个这样的视图。当我按下按钮时,我想隐藏灰色视图并将粉红色视图向上移动到其空间中。当我再次按下按钮时,我希望灰色视图重新出现并将粉红色视图移回其原始位置。

我可以通过做类似的事情来做到这一点

- (IBAction)toggle:(id)sender {
    if (self.top.hidden) {
        self.top.hidden = NO;
        self.top.layer.frame = CGRectMake(0, 0, 320, 50);
        self.bottom.layer.frame = CGRectMake(0, 50, 320, 50);
    } else {
        self.top.layer.frame = CGRectMake(0, 0, 0, 0);
        self.bottom.layer.frame = CGRectMake(0, 0, 320, 50);
        self.top.hidden = TRUE;
    }
}

但是,据我了解,关于自动布局的一般智慧是不要在代码中以编程方式设置帧大小。所以我的问题是,如何使用自动布局实现相同的结果?

基本上就是这个问题的iPhone版:OS X Cocoa Auto Layout hidden elements

【问题讨论】:

    标签: ios7 autolayout


    【解决方案1】:

    找到这个问题的答案:Animating an image view to slide upwards

    简而言之:

    - (IBAction)toggle:(id)sender {
        if (self.top.hidden) {
            self.top.hidden = NO;
            [UIView animateWithDuration:0
                             animations:^{
                                 self.verticalSpace.constant += 50.0;
                                 [self.view layoutIfNeeded];
                             }];
        } else {
            [UIView animateWithDuration:0
                             animations:^{
                                 self.verticalSpace.constant -= 50.0;
                                 [self.view layoutIfNeeded];
                             }];
            self.top.hidden = TRUE;
        }
    }
    

    其中self.verticalSpace 是底视图到顶视图之间的约束集。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-27
      • 1970-01-01
      相关资源
      最近更新 更多