Reloading tableView while context menu for cell is shown breaks cell's contents, bug behaviour differs on platforms.
Create default storyboard project and replace ViewController.swift with this code. See willEnterForeground
import UIKit
final class ViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let vc = TableViewController()
present(vc, animated: false)
}
}
final class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(
self,
selector: #selector(willEnterForeground),
name: UIApplication.willEnterForegroundNotification,
object: nil
)
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
}
@objc func willEnterForeground() {
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) {
// open context menu for cell
// hide app
// open it, wait 1 sec
// displayed cell is wrong:
self.tableView.reloadData() // only iOS 15
//
// this one also breaks selected cell (iOS 14/15):
//
// self.tableView.reloadRows(at: [self.tableView.indexPathForSelectedRow!], with: .automatic)
}
}
var data = (1...30)
override func numberOfSections(in tableView: UITableView) -> Int {
1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
data.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)
cell.textLabel?.text = "\(indexPath.row)"
return cell
}
override func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
}
override func tableView(
_ tableView: UITableView,
contextMenuConfigurationForRowAt indexPath: IndexPath,
point: CGPoint
) -> UIContextMenuConfiguration? {
let actionProvider: UIContextMenuActionProvider = { _ in
UIMenu(title: "Title", children: [
UIAction(title: "Share") { _ in },
])
}
return UIContextMenuConfiguration(
identifier: nil,
previewProvider: nil,
actionProvider: actionProvider
)
}
}
Post
Replies
Boosts
Views
Activity
Tested in iOS 15.5 simulator
When pushing view controller with navigation bar over one that hasn't navigation bar, contents will be glitchy pinned to left after push animation ends.
Create a new project and replace ViewController.swift:
import SwiftUI
struct SampleView: View {
var navigationBarHidden = false
var body: some View {
Text("I'm gonna glitchy pin to left")
.frame(maxWidth: .infinity, alignment: .leading)
.navigationBarHidden(navigationBarHidden)
}
}
final class ViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let navigation = UINavigationController(
rootViewController: UIHostingController(
rootView: SampleView(navigationBarHidden: true)
)
)
navigation.modalPresentationStyle = .fullScreen
present(navigation, animated: false) {
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) {
let hosting = UIHostingController(rootView: SampleView())
navigation.pushViewController(hosting, animated: true)
}
}
}
}