【问题标题】:Collection View Covering Tab Bar集合视图覆盖选项卡栏
【发布时间】:2017-02-09 00:32:55
【问题描述】:

我有一个带有三个选项的标签栏,当我尝试转到个人资料页面时,底部的标签栏被我以编程方式添加的集合视图所覆盖。如何确保标签栏是不在此系列中?

这是 ProfilePageVC:

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)


        let flowLayout = UICollectionViewFlowLayout()

        let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: flowLayout)
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "collectionCell")
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = UIColor.cyan

        // self.lowerView.addSubview(collectionView)
        self.lowerView.insertSubview(collectionView, belowSubview: self.view)
        self.reloadInputViews()

    }

这是我的标签条码所在的位置(它只是在常规的默认 VC 中)

class ViewController: UIViewController {

    @IBOutlet weak var contentView: UIView!

    var homeVC : UIViewController!
    var testVC : UIViewController!
    var profileVC : UIViewController!

    var viewControllers : [UIViewController]!

    //What tab we are pressing
    var selectedVC : Int = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        let storyboard = UIStoryboard(name: "Main", bundle: nil)

        homeVC = storyboard.instantiateViewController(withIdentifier: "HomePage")
        testVC = storyboard.instantiateViewController(withIdentifier: "RecordPage")
        profileVC = storyboard.instantiateViewController(withIdentifier: "ProfilePage")

        viewControllers = [homeVC, testVC, profileVC]

        buttons[selectedVC].isSelected = true
        hasPressedTab(buttons[selectedVC])
    }

    @IBOutlet var buttons: [UIButton]!

    @IBAction func hasPressedTab(_ sender: UIButton) {

        let previousVC = selectedVC
        selectedVC = sender.tag
        buttons[previousVC].isSelected = false

        let previousIndex = viewControllers[previousVC]

        //removes from previous VC
        previousIndex.willMove(toParentViewController: nil)
        previousIndex.view.removeFromSuperview()
        previousIndex.removeFromParentViewController()

        //accesses current button selected
        sender.isSelected = true

        let vc = viewControllers[selectedVC]
        addChildViewController(vc)

        //addjusts size of VC to match the added contentView
        vc.view.frame = contentView.bounds
        contentView.addSubview(vc.view)
        vc.didMove(toParentViewController: self)
    }

以下是描述问题的图片:

棕褐色区域是lowerView

With-Out Collection View 作为子视图: https://ibb.co/gH2dWF

以 CollectionView 作为子视图: https://ibb.co/jWh1ka

【问题讨论】:

  • 为什么要在 Rune-Server bruh 上发布 iOS 问题

标签: ios swift3 uicollectionview xcode8


【解决方案1】:

我不确定“lowerView”指的是什么,但是您是否尝试过仅将子视图添加到视图中?

self.view.addSubview(collectionView)

【讨论】:

  • 是的,我有,这就是目前的情况,但是它覆盖了我的标签栏,我在原始帖子中包含了两张照片来解释更多情况
【解决方案2】:

我认为问题可能出在这一行:

self.lowerView.insertSubview(collectionView, belowSubview: self.view)

你为什么要在lowerView中添加它?我实际上看不到代码中 lowerView 的实际含义,但正常流程是将 collectionView 添加为 self.view 的子视图。也许 lowerView 框架实际上是在 tabBar 上方,这就是它覆盖标签栏的原因。

代码是:

self.view.addSubview(collectionView)

如果需要将collection view发送到后面,可以发送:

self.view.sendSubview(toBack: collectionView)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2022-11-23
  • 1970-01-01
  • 1970-01-01
  • 2021-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-04
相关资源
最近更新 更多