I had applied UISheetPresentationController on a ViewController and there is a back Button which is called as imageWithLabel. I want it to work properly. But problem is the sheet because sheet has to be appear every time. Means it had to appear every time when I appear on this ViewController.
The Code for this:
import UIKit
class SelectedCellViewController: UIViewController {
private let imageWithLabel = ImageViewWithLabel(text: "Sleep", image: "Back_button", imageWidth: 10, imageHeight: 18, textColor: .appIndicatorColor, font: .sfProSemibold(size: 17) ?? UIFont.systemFont(ofSize: 5))
private let backgroundImage = ImageView(image: "GuitarCamp-Big")
private let moodImage = ImageView(image: "Mood_statistics")
private let detailedView = View(backgroundColor: .appBackgroundColor)
let detailController = SongDetailViewController()
override func viewDidLoad() {
super.viewDidLoad()
setupViews()
configureUI()
let customDetent = UISheetPresentationController.Detent.custom(identifier: .none) { [weak self] context in
guard let self = self else { return 0.0 }
return self.view.frame.height - 120
}
let bottomDetent = UISheetPresentationController.Detent.custom {
[weak self] context in
guard let self = self else { return 0.0 }
return self.view.frame.height - 600
}
if let sheet = detailController.sheetPresentationController {
sheet.detents = [bottomDetent, customDetent]
sheet.prefersGrabberVisible = true
sheet.preferredCornerRadius = 20
}
detailController.isModalInPresentation = true
self.present(detailController, animated: true)
}
private func setupViews() {
view.addSubview(backgroundImage)
view.addSubview(moodImage)
view.addSubview(imageWithLabel)
view.addSubview(detailedView)
NSLayoutConstraint.activate([
imageWithLabel.topAnchor.constraint(equalTo:view.topAnchor, constant: 40),
imageWithLabel.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 5.autoSize),
imageWithLabel.widthAnchor.constraint(equalToConstant: 60.autoSize),
imageWithLabel.heightAnchor.constraint(equalToConstant: 30.autoSize),
backgroundImage.topAnchor.constraint(equalTo: view.topAnchor),
backgroundImage.leadingAnchor.constraint(equalTo: view.leadingAnchor),
backgroundImage.trailingAnchor.constraint(equalTo: view.trailingAnchor),
backgroundImage.bottomAnchor.constraint(equalTo: view.bottomAnchor),
moodImage.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -60.autoSize),
moodImage.centerXAnchor.constraint(equalTo: view.centerXAnchor),
moodImage.widthAnchor.constraint(equalToConstant: 293.autoSize),
moodImage.heightAnchor.constraint(equalToConstant: 108.autoSize),
])
}
private func configureUI() {
imageWithLabel.isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(backToMainViewController))
imageWithLabel.addGestureRecognizer(tapGesture)
}
@objc private func backToMainViewController() {
self.navigationController?.popViewController(animated: true)
print("true")
}
How would I add back to popViewController? I’m talking about the SelectedViewController, not the presenting Controller. Back Button should be there because it is in the design, and also I want a tabBarController which is created and used in other Controllers and I also want to add on this.