簡體   English   中英

使用約束和動畫隱藏視圖

[英]Hiding a view with constraints and animation

我有一個UIViewController它是一個自定義TabBarController 在里面我有一個UIView ,它包含UIViewController和底部的另一個UIView作為TabBar ,我想隱藏帶有動畫的TabBar並保持約束有序。 出於某種原因,每次我嘗試這樣做時,視圖約束都會變得一團糟。

TabBar具有 100 點的恆定高度。

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

幫助?

我猜最好的方法是將標簽欄高度約束設為 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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM