Created
January 12, 2022 19:57
-
-
Save auramagi/a5af70718b0e31af37403cd607b489d9 to your computer and use it in GitHub Desktop.
UIRefreshControl stuck when changing tabs in UITabBarController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
@main | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { | |
func makeRefreshControl() -> UIRefreshControl { | |
let refresh = UIRefreshControl() | |
let action = UIAction { [weak refresh] _ in | |
DispatchQueue.main.asyncAfter(deadline: .now() + 5) { | |
refresh?.endRefreshing() | |
} | |
} | |
refresh.addAction(action, for: .valueChanged) | |
return refresh | |
} | |
let tc = UITabBarController() | |
tc.viewControllers = [ | |
{ | |
let vc = UITableViewController(style: .plain) | |
vc.tableView.dataSource = self | |
vc.refreshControl = makeRefreshControl() | |
vc.title = "One" | |
vc.tabBarItem.image = .init(systemName: "1.circle") | |
return vc | |
}(), | |
{ | |
let vc = UIViewController() | |
vc.title = "Two" | |
vc.tabBarItem.image = .init(systemName: "2.circle") | |
return vc | |
}() | |
] | |
window?.rootViewController = tc | |
tc.view.backgroundColor = .systemBackground | |
return true | |
} | |
} | |
extension AppDelegate: UITableViewDataSource { | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
100 | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = UITableViewCell(style: .default, reuseIdentifier: nil) | |
cell.textLabel?.text = "Cell \(indexPath.row)" | |
return cell | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment