首页 > 解决方案 > 隐藏带有约束和动画的视图

问题描述

我有UIViewController一个习惯TabBarController。在里面我有一个UIViewwhichUIViewController和在底部另一个UIView作为 a 的功能TabBar,我想隐藏TabBarwith 动画并保持约束有序。出于某种原因,每次我尝试这样做时,视图约束都会变得一团糟。

具有 100 点的TabBar恒定高度。

- UIViewController
  - viewContent (UIView, the UIViewController container)
  - viewTabBar (UIView as TabBar)

在此处输入图像描述

这是我的代码:

func hideTabBar() {
    UIView.animate(withDuration: 400) {
        self.contentView.frame.size.height += self.viewTabBar.frame.size.height
        self.view.layoutIfNeeded()
    }
}

帮助?

标签: iosswiftuiviewconstraintsuiviewanimation

解决方案


我猜想做到这一点的最佳方法是将标签栏高度约束设为 IBOutlet,或者只是以编程方式创建它,然后只需更改该约束常量值。像这样的东西:

var tabbarHeightConstraint = NSLayoutConstraint(item: tabbarView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .none, multiplier: 1, constant: 100)

NSLayoutConstraint.activate([tabbarHeightConstraint])

// Animating, set new tab bar height to 0
tabbarHeightConstraint.constant = 0

UIView.animate(withDuration: 400) {
  self.view.layoutIfNeeded() // Or wherever the tabbar view is in
}

现在,如果您想再次显示标签栏,只需执行相同操作,但将常量设置为 100。


推荐阅读