gpt4 book ai didi

ios - 如何在不制作 IBOutlet 的情况下以编程方式在 Swift 中为 UIView 高度约束设置动画?

转载 作者:搜寻专家 更新时间:2023-11-01 06:13:07 24 4
gpt4 key购买 nike

我创建了一个附在顶部、前导和尾随且高度固定的按钮。当我点击按钮时,新的 UIView 实例应该会出现动画。

import UIKit

class ViewController: UIViewController {

var topButton = UIButton()
var myView = UIView()

override func viewDidLoad() {
super.viewDidLoad()

topButton = UIButton(type: .roundedRect)
topButton.setTitle("Add Acctivity", for: .normal)
topButton.backgroundColor = UIColor.purple
topButton.setTitleColor(UIColor.white, for: .normal)
topButton.titleLabel?.font = UIFont(name: "System", size: 26)
topButton.translatesAutoresizingMaskIntoConstraints = false
topButton.addTarget(self, action: #selector(expandMenu), for: .touchUpInside)
view.addSubview(topButton)

topButton.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
topButton.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
topButton.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
topButton.heightAnchor.constraint(equalToConstant: 70).isActive = true

myView.backgroundColor = UIColor.blue
myView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(myView)
myView.topAnchor.constraint(equalTo: view.topAnchor, constant: 70).isActive = true
myView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
myView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
myView.heightAnchor.constraint(equalToConstant: 100).isActive = true

}

@objc func expandMenu() {

myView.heightAnchor.constraint(equalToConstant: 300).isActive = true

UIView.animate(withDuration: 1) {

self.view.layoutIfNeeded()
}
}



}

Nothing is working even I put the height to be 300

有趣的是,当我将高度设置为小于其初始值时,动画会起作用。

enter image description here

当人们从 Storyboard创建 IBOutlet 然后更改其常量值时,我看到了很多教程。但我想知道如何以编程方式做到这一点。

最佳答案

您的代码中的问题是您正在尝试重新激活另一个描述 myView 高度的约束,通过说(在 expandMenu() 中):

myView.heightAnchor.constraint(equalToConstant: 300).isActive = true

我假设您收到类似于以下内容的运行时警告:

[LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. ( "NSLayoutConstraint:0x60400008e5b0 UIView:0x7fa16a419450.height == 100 (active)>", "NSLayoutConstraint:0x608000097e30 UIView:0x7fa16a419450.height == 300 (active)>" )

Will attempt to recover by breaking constraint

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.

表示myView已经有高度约束,即(在 viewDidLoad() 中):

myView.heightAnchor.constraint(equalToConstant: 100).isActive = true

您应该做的是更改 constant取而代之的是相同约束的值。你可以这样实现:

声明一个 NSLayoutConstraint作为实例变量:

var myViewHeightConstraint: NSLayoutConstraint!

并在viewDidLoad() :

myViewHeightConstraint = NSLayoutConstraint(item: myView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 0.0, constant: 100)
myViewHeightConstraint.isActive = true

代替:

myView.heightAnchor.constraint(equalToConstant: 100).isActive = true

最后,在 expandMenu() :

myViewHeightConstraint.constant = 300

代替:

myView.heightAnchor.constraint(equalToConstant: 300).isActive = true

输出:

enter image description here


此外:

如果您的目标是让同一个按钮实现展开/折叠行为,您只需将其实现为(在 expandMenu() 中):

myViewHeightConstraint.constant = myViewHeightConstraint.constant == 300 ? 100 : 300

关于ios - 如何在不制作 IBOutlet 的情况下以编程方式在 Swift 中为 UIView 高度约束设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50961877/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com