gpt4 book ai didi

swift - 加载 Controller 后如何以编程方式添加 View

转载 作者:行者123 更新时间:2023-11-30 10:40:45 25 4
gpt4 key购买 nike

我有一个 ViewController,在加载时带有隐藏 View (我们称之为 boxInsideView)。我希望这个 boxInsideView 的 Controller 或 .swift 文件在用户单击该框时触发,而不是在 View Controller 加载时触发。就像我在单击该框时创建一个 addSubview(boxInsideView) 一样。

enter image description here

实例化我的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 文件就会被自动触发。

我想要做的是添加这个 View ,还有它的约束和单击该框时的动画。

我的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 后,您可以设置第二个 view.isHidden = true 的默认值。如果您点击第一个 View ,则会设置另一个 view.isHidden = false

关于swift - 加载 Controller 后如何以编程方式添加 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56841333/

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