ios - swift:在 ViewController 返回后显示动画

标签 ios swift

我有第一个ViewController和第二个ViewController。当我从第二个 ViewController 返回到第一个 ViewController 时,我想在第一个 ViewController 中显示按钮。

要返回,我使用以下代码:

self.dismiss(animated: true, completion: nil)

此代码用于在第一个 ViewController 中显示按钮

@IBOutlet weak var infoButton: UIButton!
@IBOutlet weak var nextButton: UIButton!

@IBOutlet weak var infoButtonTopConstraint: NSLayoutConstraint!
@IBOutlet weak var nextButtonBottomConstraint: NSLayoutConstraint!
@IBOutlet weak var infoButtonHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var nextButtonHeightConstraint: NSLayoutConstraint!

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)

    showButtonAnimation()
    //animaton()
}

func showButtonAnimation() {

    self.infoButtonTopConstraint.constant += self.infoButtonHeightConstraint.constant
    self.nextButtonBottomConstraint.constant -= self.nextButtonHeightConstraint.constant

    UIView.animate(withDuration: 1.0, delay: 0.0,
                   options: [], animations: {
                    self.view.layoutIfNeeded()

    })
}

但是当我从第二个 ViewController 返回时,按钮没有显示。

最佳答案

替换这个

func showButtonAnimation() {
    self.view.layoutIfNeeded()
    UIView.animate(withDuration: 1.0, delay: 0.0,
                       options: [], animations: {
        self.infoButtonTopConstraint.constant += self.infoButtonHeightConstraint.constant
        self.nextButtonBottomConstraint.constant -= self.nextButtonHeightConstraint.constant
        self.view.layoutIfNeeded()

    })
}

编辑 1

viewWillAppear 未调用原因:您将第二个 viewController 呈现为 overCurrentContext modalPresentationStyle

只需将 overCurrentContext 替换为 fullScreen 即可。

关于ios - swift:在 ViewController 返回后显示动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51605669/

相关文章:

ios - 如何在 IOS 中将回调方法作为参数传递

ios - 从通用 MTLBuffer 读取内容?

nsstring - 为什么在转换为 float 或 double 时出现 EXC_BAD_ACCESS 错误? (在 swift 中)

ios - 尝试给我们 UIAlertController 打分

ios - 在 macOS Safari 和 iOS 浏览器中悬停时出现 CSS 转换和 z-index 错误

ios - NSFetchedResultsController 不更新更新 NSManagedObjects

ios - 为 Objective C TableView 添加加载功能

IOS13 : UIActivityViewController. 选择保存照片后未调用completionWithItemsHandler

arrays - 在 UITableView 的数组中搜索类的属性

ios - 如何拥有类似下面的枚举,但要动态配置而不是静态配置?