【发布时间】:2022-01-14 10:31:41
【问题描述】:
当用户单击 tableView 单元格时,我想推送一个视图控制器,以显示用户任务的详细视图。我尝试使用弹出视图,因为我不知道如何从 collectionView 单元中推送 viewController,但我无法将 didselect 连接到 MyTasksDetailController 布局视图。这是我的代码:
MyTasksCollectionCell
class MyTasksCollectionCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let detailView = MyTasksDetailController()
UIApplication.shared.keyWindow?.addSubview(detailView)
// I want the detailViewController to show these two (descriptionTitleLabel + titleLabel)
// print("\(task.descriptionTitleLabel)")
// print("\(task.titleLabel)")
}
extension UIApplication {
var keyWindow: UIWindow? {
// Get connected scenes
return UIApplication.shared.connectedScenes
// Keep only active scenes, onscreen and visible to the user
.filter { $0.activationState == .foregroundActive }
// Keep only the first `UIWindowScene`
.first(where: { $0 is UIWindowScene })
// Get its associated windows
.flatMap({ $0 as? UIWindowScene })?.windows
// Finally, keep only the key window
.first(where: \.isKeyWindow)
}
}
MyTasksDetailController
class MyTasksDetailController: UIView {
var setTitleLabel: String? {
didSet {
titleLabel.text = setTitleLabel ?? ""
}
}
var setdescriptionTitleLabel: String? {
didSet {
descriptionTitleLabel.text = setdescriptionTitleLabel ?? ""
}
}
let titleLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont.systemFont(ofSize: 28, weight: .bold)
label.textAlignment = .center
return label
}()
let descriptionTitleLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont.systemFont(ofSize: 18, weight: .bold)
label.textAlignment = .center
label.numberOfLines = 3
return label
}()
let container: UIView = {
let v = UIView()
v.translatesAutoresizingMaskIntoConstraints = false
v.clipsToBounds = true
v.backgroundColor = .white
v.backgroundColor =
// 1
UIColor { traitCollection in
// 2
switch traitCollection.userInterfaceStyle {
case .dark:
// 3
v.layer.borderColor = UIColor.label.cgColor
return UIColor.systemBackground
default:
// 4
v.layer.borderColor = UIColor.black.cgColor
return UIColor.systemBackground
}
}
return v
}()
lazy var stack: UIStackView = {
let stack = UIStackView(arrangedSubviews: [titleLabel, descriptionTitleLabel])
stack.translatesAutoresizingMaskIntoConstraints = false
stack.axis = .vertical
return stack
}()
@objc func animateOut() {
UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseOut, animations: {
self.container.transform = CGAffineTransform(translationX: self.frame.height, y: 0)
self.alpha = 0
}) { (complete) in
if complete {
self.removeFromSuperview()
}
}
}
@objc func animateIn() {
self.container.transform = CGAffineTransform(translationX: self.frame.height, y: 0)
self.alpha = 1
UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: .curveEaseOut, animations: {
self.container.transform = .identity
self.alpha = 1
})
}
override init(frame: CGRect) {
super.init(frame: frame)
self.frame = UIScreen.main.bounds
self.addSubview(container)
self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(animateOut)))
container.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true
container.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = true
container.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
container.addSubview(stack)
stack.leadingAnchor.constraint(equalTo: container.leadingAnchor).isActive = true
stack.trailingAnchor.constraint(equalTo: container.trailingAnchor).isActive = true
stack.centerYAnchor.constraint(equalTo: container.centerYAnchor).isActive = true
stack.heightAnchor.constraint(equalTo: container.heightAnchor, multiplier: 0.5).isActive = true
animateIn()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
【问题讨论】:
-
MyTasksDetailController是UIView,而不是UIViewController...您在导航控制器中并且想要push一个“详细视图控制器”到堆栈上吗?或者,您想present一个“详细视图控制器”吗?还是只想将MyTasksDetailController添加为子视图? -
@DonMag 我不在导航控制器中,是的,我想展示一个详细的视图控制器
标签: ios swift xcode uitableview uicollectionviewcell