首页 > 解决方案 > 在基本 VC 中按下按钮时,如何更改容器视图内的容器框架?

问题描述

我在基本视图控制器中有一个容器视图。当 viewWillApear 我更改容器视图的框架时,它不会出现在屏幕上。在基础 VC 上有一个按钮,当按下该按钮时应该“调出”之前向下移动的 VC。我怎样才能做到这一点?

我已经尝试在 IBAction 中为基本 VC 中的按钮调用用于更改帧的方法,但是当我这样做时,我得到了一个 runTimeError:

    @objc func CommentsTapped(_ tap: UITapGestureRecognizer) {
    //Bring up the comments view and load all data into it.
    UIView.animate(withDuration: 0.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.9, options: .curveEaseOut, animations: {

        self.commentsDelegate!.updateCommentSheet(frame: self.initalFrame.offsetBy(dx: 0, dy: 227))//This method in implemented in the base VC given that it is specified in the protocol for the commentsVC
    })
}

线程 1:致命错误:在展开可选值时意外发现 nil

方法的实现:

class BaseVC: UIViewController, CommentCellDelegate {

@IBOutlet weak var commentsContainer: UIView!

func updateCommentSheet(frame: CGRect) {
    commentsContainer.frame = frame
}

标签: iosswiftcontainers

解决方案


基本上,你想要做的是:

创建一个 var 并将其设置为与您的 prepare 方法中的 ViewController 相同。现在您可以访问并且可以从您的基本视图控制器/类控制第二个视图控制器/类。因此,如果您的工作表委托设置正确(即 ViewController.xdelegate = self),那么您可以这样调用它:

self.secondVC.xdelegate.updateFrame(frame: newFrame)

推荐阅读