我有一个TabBarController作为我的rootViewController。第一个选项卡是一个带有导航栏的tableView。当tableView执行其获取请求并加载其内容时,我会呈现一个带有加载指示符的加载viewController。loadingVC没有覆盖tabBar,这很好,但它确实覆盖了NavBar,我想避免它。我基本上希望loadingVC被放置在NavBar和TabBar之间,这样就可以绑定它的视图框架——它的顶部到NavBar的底部,底部到TabBar的顶部。我无法使用此功能,我想我可以在.modalPresentationStyle中找到一个解决方案,但那里的选项没有涵盖我所描述的内容。
场景代理代码:
window = UIWindow(frame: UIScreen.main.bounds)
window?.windowScene = windowScene
let rootVC = TBController()
window?.rootViewController = rootVC
window?.makeKeyAndVisible()
TabBarController:
class TBController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
tabBar.isTranslucent = false
// Article TVC
let articleVC = UINavigationController(rootViewController: ArticlesTVC())
let articleIcon = UITabBarItem(title: "News", image: UIImage(systemName: "newspaper"), tag: 0)
articleVC.tabBarItem = articleIcon
articleVC.navigationBar.prefersLargeTitles = false
articleVC.navigationBar.isTranslucent = false
articleVC.navigationBar.barTintColor = .systemBackground
articleVC.definesPresentationContext = true
articleVC.view.clipsToBounds = true
}
加载VC:
class LoadingVC: UIViewController {
var loadingLabel: UILabel = {
let loadingLabel = UILabel()
loadingLabel.text = "Loading articles..."
loadingLabel.textAlignment = .center
loadingLabel.translatesAutoresizingMaskIntoConstraints = false
loadingLabel.font = .systemFont(ofSize: 18, weight: .heavy)
loadingLabel.backgroundColor = .systemBackground
return loadingLabel
}()
var loadingActivityIndicator: UIActivityIndicatorView = {
let indicator = UIActivityIndicatorView()
indicator.style = .large
indicator.color = .white
indicator.startAnimating()
indicator.translatesAutoresizingMaskIntoConstraints = false
indicator.backgroundColor = .systemBackground
return indicator
}()
var blurEffectView: UIVisualEffectView = {
let blurEffect = UIBlurEffect(style: .regular)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.clipsToBounds = true
blurEffectView.alpha = 0.8
blurEffectView.translatesAutoresizingMaskIntoConstraints = false
blurEffectView.backgroundColor = .systemBackground
return blurEffectView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
view.addSubview(loadingLabel)
view.addSubview(blurEffectView)
blurEffectView.frame = view.bounds
view.addSubview(loadingActivityIndicator)
loadingActivityIndicator.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
loadingActivityIndicator.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
view.addSubview(loadingLabel)
loadingLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
loadingLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 50).isActive = true
}
}
TableView:
class ArticlesTVC: UITableViewController {
let loadingVC = LoadingVC()
override func viewDidLoad() {
super.viewDidLoad()
loadingVC.modalPresentationStyle = .currentContext
loadingVC.modalTransitionStyle = .crossDissolve
DispatchQueue.main.async {
self.present(self.loadingVC, animated: true)
}
tableView.separatorStyle = .none
tableView.backgroundColor = .systemBackground
}
}
您最好将其作为tableView上方的视图,并在加载内容后将true赋值给其isHidden属性。故事板中有点混乱,尽管有很多方法可以解决这个问题,但在您的情况下,无论如何,您都是以编程方式添加到视图控制器的视图中,所以将代码添加到自定义视图中,然后将该视图添加到表视图中应该没有问题。然后,您可以随意调用自定义视图的方法。