ios - 在基本 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
}
解决方案
基本上,你想要做的是:
创建一个 var 并将其设置为与您的 prepare 方法中的 ViewController 相同。现在您可以访问并且可以从您的基本视图控制器/类控制第二个视图控制器/类。因此,如果您的工作表委托设置正确(即 ViewController.xdelegate = self),那么您可以这样调用它:
self.secondVC.xdelegate.updateFrame(frame: newFrame)
推荐阅读
- sql - 基于用户定义标记的反向跟踪强制记录关系
- python - 如何让不和谐机器人说出接下来播放的歌曲
- properties - 当某些顶点/边缘中不存在预期属性时如何返回 NULL/空白空间
- python-3.x - 无法使用 PYTHONPATH 设置 python 模块搜索路径
- python - ImportError:没有名为 click 的模块
- java - 如何更改滚动视图最大拉伸指示器的颜色
- java - 如何解决 java.lang.module.ResolutionException:模块 stax.api 和 java.xml 将包 javax.xml 导出到模块 log4j
- verilog - 在系统verilog的嵌套for循环中初始化和增加一个genvar变量
- rtmp - 如何将 RTMP url 的查询部分发送到 RTMP 服务器
- javascript - Google Maps API:如何捕捉“DIRECTIONS_ROUTE:NOT_FOUND”?