[英]How animate UIView height constraint in Swift programmatically without making an IBOutlet?
[英]How to update the constant height constraint of a UIView with IBOutlet
我正在嘗試更改菜單視圖的高度以使其打開,但是當我添加 NSLayoutConstraint 的 IBOutlet 時,它會以相同的錯誤代碼使我的應用程序崩潰。 我三次檢查插座是否正確連接,所以這不是問題。 我是否遺漏了什么,或者這是否在新的 Xcode 中更新,因為所有其他問題都來自至少 5 年前。
謝謝!
錯誤信息:
線程 1:“[<UIViewController 0x7f8fe2705330> setValue:forUndefinedKey:]:該類不符合 key menuHeightConstraint 的鍵值編碼。”
相關代碼:
class HomeViewController: UIViewController {
// MARK: IBOutlets
@IBOutlet weak var menuHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var menuView: UIView!
// MARK: Variables
var menuIsOpen = false
// MARK: IBaction functions
@IBAction func openMenu() {
menuIsOpen.toggle()
menuHeightConstraint.constant = menuIsOpen ? 180 : 55
UIView.animate(
withDuration: 1,
delay: 0,
options: .curveEaseIn,
animations: { self.menuView.layoutIfNeeded() },
completion: nil)
}
}
目前尚不清楚您的menuView
插座是否已連接,但從您包含的屏幕截圖來看,它似乎不是。 在這種情況下,它會崩潰,因為當您嘗試調用self.menuView.layoutIfNeeded()
時menuView
將為 nil。 無論哪種方式,您都應該調用self.view.layoutIfNeeded()
來代替。
這似乎是一個 Xcode 錯誤,第二天我重新啟動了項目,它運行良好。 所以如果你想用動畫編輯約束,你可以像上面看到的那樣做:-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.