【问题标题】:Swift - Have a "Loading" ViewController in between the NavBar and TabBar while TableView loads its contentsSwift - 在 NavBar 和 TabBar 之间有一个“正在加载”的 ViewController,而 TableView 会加载其内容
【发布时间】:2021-06-29 16:39:54
【问题描述】:

我有一个 TabBarController 作为我的 rootViewController。第一个选项卡是带有 NavigationBar 的 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()

标签栏控制器:

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
    
    
}
}

表格视图:

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
}
}

【问题讨论】:

    标签: ios swift uiviewcontroller uikit


    【解决方案1】:

    您最好将其作为 tableView 上方的视图,并在您的内容加载后为其 isHidden 属性分配 true。故事板有点乱,虽然有一些方法可以解决这个问题,但在你的情况下,无论如何你都是以编程方式添加到视图控制器的视图中,所以将你的代码添加到自定义视图,然后在你的表格视图上添加该视图应该没问题。然后,您可以随意调用自定义视图的方法。

    【讨论】:

    • 我喜欢这个想法,但我无法让它发挥作用。我已经将 LoadingVC 代码移到了 LoadingView UIView 中。我在 ArticlesTVC 中创建了一个全局 loadingView 变量,并尝试在 viewDidLoad 和 viewWillAppear 中添加 loadingView 作为 tableView 或 navigationController.view 的子视图,但它不会显示。有什么想法吗?
    • 您可以将其添加为 ArticlesTVC 视图的子视图。您可能必须为其分配正确的 z 值(它在屏幕中的前进距离)。您是否尝试将其添加为 ArticlesTVC 视图的子视图?
    • 另外,我会避免不必要的全局,只是让您的视图成为 ArticlesTVC 的属性,然后将其 isHidden 属性分配为 true 或 false。抱歉,我的用处不大,但我习惯使用故事板。
    • 我尝试将它添加为 ArticlesTVC 的子视图,然后为其分配 z 值,但由于某种原因它仍然不会显示。用 navController.view 和 tableView 也试过了,但没有运气。不过欣赏这些想法!
    • 您确定 tableView 的 z 值较小吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    相关资源
    最近更新 更多