我在tableview中使用了UIRefreshControl,它在ios 13中自动呈现了视图控制器。使用了同一视图控制器中的tableview。
vc.modalPresentationStyle = .automatic
问题是,只要刷新刷新,视图控制器就会立即关闭。我已阻止以下代码关闭,但视图控制器仍然无法从顶部刷新。
vc.isModalInPresentation = true
如何刷新表视图而不关闭视图控制器?
解决方案是在显示视图控制器之前,使用可用的UITableView
选项全屏显示包含UICollectionView
或modalPresentationStyle
的视图控制器:
yourViewController.modalPresentationStyle = UIModalPresentationFullScreen;
yourViewController.modalPresentationStyle = .fullScreen
Objective-C中的完整示例:
NSString *storyboardName = @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
// YourViewController is the VC that contains a table view or collection view with a refresh control.
YourViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"YourViewControllerIdentifier"];
// Or not using storyboard
// YourViewController *vc = [YourViewController new];
vc.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:vc animated:YES completion:nil];
使用其他modalPresentationStyle
选项进行实验以满足您的需求。
Apple Docs中的更多信息。
如果iOS版本是10以后的版本,请使用tableView的RefreshControl,不要使用addSubview。
let refreshControl:UIRefreshControl = UIRefreshControl.init()
if #available(iOS 10.0, *) {
tableView.refreshControl = refreshControl
} else {
tableView.addSubview(refreshControl)
}
无论isModalInPresentation是对还是错。