Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save treastrain/04afea20f29d600200b326502ac7568e to your computer and use it in GitHub Desktop.
Save treastrain/04afea20f29d600200b326502ac7568e to your computer and use it in GitHub Desktop.
`UISheetPresentationController.prefersGrabberVisible` が `true` だったり、`View.presentationDragIndicator(_:)` が `.visible` だと deinit されない https://twitter.com/treastrain/status/1669271711269945344
//
// ViewController.swift
// SheetPresentationControllerPlayground
//
// Created by Ryoga Tanaka on 2023/06/15.
//
import UIKit
@available(iOS 16.4, *)
final class ViewController: UIViewController {
@ViewLoading private var button: UIButton
override func viewDidLoad() {
super.viewDidLoad()
var configuration = UIButton.Configuration.plain()
configuration.title = "Tap me!"
button = .init(configuration: configuration, primaryAction: .init { [unowned self] _ in
let viewController = PresentedViewController()
viewController.sheetPresentationController?.prefersGrabberVisible = true
present(viewController, animated: true)
})
button.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(button)
NSLayoutConstraint.activate([
button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
button.centerYAnchor.constraint(equalTo: view.centerYAnchor),
])
}
}
final class PresentedViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemGreen
}
override func viewDidDisappear(_ animated: Bool) {
print(self, #function, "is called")
super.viewDidDisappear(animated)
}
deinit {
print(self, "is deinited") // NOT CALLED HERE IF `sheetPresentationController.prefersGrabberVisible` IS `true` !!!
}
}
//
// ContentView.swift
// PresentationDetentsPlayground
//
// Created by Ryoga Tanaka on 2023/06/15.
//
import SwiftUI
final class Object: ObservableObject {
init() {
print(self, "is inited")
}
deinit {
print(self, "is deinited")
}
}
struct ContentView: View {
@State private var isPresented = false
var body: some View {
Button("Tap me!") {
isPresented = true
}
.sheet(isPresented: $isPresented) {
SheetView()
}
}
}
struct SheetView: View {
@StateObject private var object = Object()
var body: some View {
Text("Hello, happy world!")
.presentationDetents([.medium])
.presentationDragIndicator(.visible) // If this is `.hidden` or this modifier is removed, the `object` will be correctly `deinit`.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment