swift - 如何在控制器加载后以编程方式添加视图
问题描述
我在加载ViewController
时有一个隐藏视图(我们称之为, )。boxInsideView
我希望 thisboxInsideView
的控制器或.swift
文件在用户单击框时触发,而不是在视图控制器加载时触发。就像我会addSubview(boxInsideView
在单击该框时进行操作一样。
实例化我的boxInsideView
let cajasInsideView : CajaInsideView = {
let cajasInside = CajaInsideView()
cajasInside.delegate = self
return cajasInside
}()
我viewDidLoad()
有这个代码:
scrollView.addSubview(cajasInsideView)
scrollView.addSubview(cajasView)
scrollView.addSubview(collectionView)
cajasView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 0).isActive = true
cajasView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 0).isActive = true
cajasView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
cajasView.heightAnchor.constraint(equalToConstant: 150).isActive = true
if !isClosed {
cajasInsideView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 0).isActive = true
cajasInsideView.autoPinEdge(.top, to: .bottom, of: cajasView, withOffset: -10)
cajasInsideView.heightAnchor.constraint(equalToConstant: 0).isActive = true
cajasInsideView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
}
topConstraint = NSLayoutConstraint(item: collectionView, attribute: .top, relatedBy: .equal, toItem: cajasView, attribute: .bottom, multiplier: 1, constant: 0)
scrollView.addConstraint(topConstraint!)
collectionView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 0).isActive = true
collectionView.heightAnchor.constraint(equalToConstant: 1000).isActive = true
collectionView.widthAnchor.constraint(equalTo: scrollView.widthAnchor).isActive = true
一旦scrollView.addSubview(cajasInsideView)
被触发,我的.swift
文件就会自动被触发。
我想要做的是添加这个视图,它的约束和动画在点击那个框时。
我的openBox()
功能:
@objc func openBox(){
if isClosed {
shareDropDown.dataSource = ["Añadir objetos a esta caja", "Añadir objeto nuevo a esta caja", "Sacar objeto de esta caja"]
shareDropDown.selectionAction = { [unowned self] (index: Int, item: String) in
if index == 1 {
let storyboard = UIStoryboard(name: "Trastero", bundle: nil)
let uploadTrastero = storyboard.instantiateViewController(withIdentifier: "UploadTrasteroViewController") as! UploadTrasteroViewController
uploadTrastero.navigationItem.leftItemsSupplementBackButton = true
self.navigationController?.pushViewController(uploadTrastero, animated: true)
}
if index == 0 {
self.setMoverObjeto()
}
print("Selected item: \(item) at index: \(index)")
}
shareDropDown.width = 250
scrollView.removeConstraint(topConstraint!)
topConstraint = NSLayoutConstraint(item: collectionView, attribute: .top, relatedBy: .equal, toItem: cajasInsideView, attribute: .bottom, multiplier: 1, constant: 0)
UIView.animate(withDuration: 0.5, delay:0, usingSpringWithDamping:0.7, initialSpringVelocity:0, options: .curveEaseOut, animations: {
self.cajasInsideView.frame = CGRect(x: 0, y: self.cajasView.frame.height - 40, width: self.scrollView.frame.width, height: 360)
self.collectionView.frame = CGRect(x: 0, y: (self.cajasView.frame.height + self.cajasInsideView.frame.height + 40), width: self.view.frame.width, height: 1000)
self.scrollView.addConstraint(self.topConstraint!)
self.scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.headerView.frame.height + self.cajasView.frame.height + self.cajasInsideView.frame.height + self.collectionView.frame.height)
//self.collectionView.autoPinEdge(.top, to: .bottom, of: self.cajasInsideView)
}, completion: { (completion) in
self.isClosed = false
})
}
else{
shareDropDown.dataSource = ["Añadir objeto", "Editar", "Crear nueva caja", "Cambiar ubicación"]
shareDropDown.selectionAction = { [unowned self] (index: Int, item: String) in
if index == 0 {
let storyboard = UIStoryboard(name: "Trastero", bundle: nil)
let uploadTrastero = storyboard.instantiateViewController(withIdentifier: "UploadTrasteroViewController") as! UploadTrasteroViewController
uploadTrastero.navigationItem.leftItemsSupplementBackButton = true
self.navigationController?.pushViewController(uploadTrastero, animated: true)
}
if index == 1 {
self.setEditar()
}
if index == 2 {
self.openPopUpNewBox()
}
if index == 3{
let popupController = PopUpSetDirectionController()
popupController.providesPresentationContextTransitionStyle = true
popupController.definesPresentationContext = true
popupController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext;
popupController.delegate = self
popupController.view.backgroundColor = UIColor.init(white: 0.4, alpha: 0)
self.present(popupController, animated: true, completion: nil)
}
print("Selected item: \(item) at index: \(index)")
}
shareDropDown.width = 170
scrollView.removeConstraint(topConstraint!)
topConstraint = NSLayoutConstraint(item: collectionView, attribute: .top, relatedBy: .equal, toItem: cajasView, attribute: .bottom, multiplier: 1, constant: 0)
esMoverObjeto = false
self.collectionView.reloadData()
UIView.animate(withDuration: 0.5, delay:0, usingSpringWithDamping:0.7, initialSpringVelocity:0, options: .curveEaseOut, animations: {
self.cajasInsideView.frame = CGRect(x: 0, y: self.cajasView.frame.height - 40, width: self.view.frame.width, height: 0)
self.collectionView.frame = CGRect(x: 0, y: (self.cajasView.frame.height), width: self.view.frame.width, height: 1000)
self.scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.headerView.frame.height + self.cajasView.frame.height + self.cajasInsideView.frame.height + self.collectionView.frame.height)
self.scrollView.addConstraint(self.topConstraint!)
}, completion: { (completion) in
self.isClosed = true
})
}
}
解决方案
加载 VC 后,您将默认设置为 2nd view.isHidden = true
。如果您点击第一个视图,则设置另一个view.isHidden = false
.
推荐阅读
- javascript - Angular 8 / Ionic 5 / Openlayers 6:无法在特征上居中地图
- javascript - 打开汉堡菜单时,我在网页上的位置跳回顶部
- python - 三组双打
- javascript - 如何让 yup.string() 要求任何长度的字符串(包括 0)
- php - 如何将数据从数据库 Laravel 传递到父布局
- node.js - 贝宝订阅和计费协议之间的区别?
- amazon-web-services - Lambda 未触发 SNS
- r - 在 as.POSIXct 中更改时区
- c++ - Maybe 和 Either 单子、短路和性能
- python - AttributeError:“int”对象没有属性“to_dict”