以编程方式创建的 UIViewController 不明确位置的 UIView

UIView of a UIViewController ambiguous position when created programmatically

提问人:tara tandel 提问时间:11/2/2020 最后编辑:tara tandel 更新时间:11/8/2020 访问量:385

问:

我的 .正如我从调试器中了解到的那样,原因是 .UIViewControllerUIViewUIViewController

我以编程方式创建了一个场景,场景是这样的:tagList

我有一个.在里面,我添加了排列好的子视图,即 an 和 .里面有一个.必须具有固定大小。我给出了所有需要的约束:UIStackViewstackViewUIImageUIViewControllerUIViewControllerUICollectionViewUIViewController

初始化 tagList

lazy var tagBarView: TopBarViewController = {
    let view = TopBarViewController(nibName: nil, bundle: nil)
    view.view.translatesAutoresizingMaskIntoConstraints = false
    view.view.backgroundColor = .blue
    view.view.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 48)
    view.view.clipsToBounds = true
    return view
}()

调用 tagList

self.stackView.insertSubview(self.tagBarView.view, at: 1)

UIStackView 的约束

stackView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor).isActive = true
stackView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor).isActive = true
stackView.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor).isActive = true
stackView.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor).isActive = true
    
    
stackView.axis = .vertical
stackView.alignment = .fill
stackView.distribution = .fill
stackView.spacing = 0
stackView.clipsToBounds = false

然后,我再次以编程方式在我的 viewController 中创建了 collectionView,这是 collectionView 和 FlowLayout 的设置

collectionView 和 FlowLayout 的设置

func setupUI() {
    let collectionViewFlowLayout = UICollectionViewFlowLayout()
    collectionViewFlowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
    collectionViewFlowLayout.scrollDirection = .horizontal
    topBarCollectionView = UICollectionView(frame: CGRect(x: 8, y: 8, width: 604, height: 32), collectionViewLayout: collectionViewFlowLayout)
    topBarCollectionView.clipsToBounds = true
    topBarCollectionView.contentMode = .scaleToFill
    topBarCollectionView.showsVerticalScrollIndicator = false
    topBarCollectionView.showsHorizontalScrollIndicator = false
    
    topBarCollectionView.clearsContextBeforeDrawing = false
    topBarCollectionView.register(TagCollectionViewCell.self, forCellWithReuseIdentifier: "TagCell")
    topBarCollectionView.collectionViewLayout = collectionViewFlowLayout
    topBarCollectionView.contentInsetAdjustmentBehavior = .always
    topBarCollectionView.collectionViewLayout.invalidateLayout()
    self.topBarCollectionView.delegate = self
    self.topBarCollectionView.dataSource = self
    topBarCollectionView.translatesAutoresizingMaskIntoConstraints = false
    
}

UICollectioView 的约束

func setupConstraints() {

    self.view.translatesAutoresizingMaskIntoConstraints = false
    self.view.addSubview(topBarCollectionView)

  
    self.view.safeAreaLayoutGuide.trailingAnchor.constraint(equalTo: topBarCollectionView.trailingAnchor, constant: 8).isActive = true

    self.view.safeAreaLayoutGuide.bottomAnchor.constraint(equalTo: topBarCollectionView.bottomAnchor, constant: 8).isActive = true
    
    self.view.safeAreaLayoutGuide.leadingAnchor.constraint(equalTo: topBarCollectionView.leadingAnchor, constant: -8).isActive = true
    self.view.safeAreaLayoutGuide.topAnchor.constraint(equalTo: topBarCollectionView.topAnchor, constant: -8).isActive = true

    topBarCollectionView.heightAnchor.constraint(equalToConstant: 32).isActive = true
    topBarCollectionView.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width - 16).isActive = true

    
    topBarCollectionView.reloadData()
}

结果是,在我的层次结构中,的位置不正确,所以我得到了一个黑屏。UIViewUIViewController

视图层次结构 View Hierar

最终结果 视图是顶部的黑色视图(应该是白色的) enter image description here

iOS Swift iPhone Xcode UIVieviceController

评论

0赞 DonMag 11/2/2020
试着把事情简单化......您的 Hierarchy 显示带有图像视图和 TopBarViewController 的垂直堆栈视图,但您的屏幕截图以相反的顺序显示它们(图像位于顶部栏下方)?从制作一个带有单个标签的普通视图控制器开始......你有白色背景吗?顺便说一句...... 阅读起来非常困难......调用视图控制器也很令人困惑......是 ?是 ?TopBarViewControllerview.viewtagBarViewUIViewUIViewController

答:

0赞 tara tandel 11/8/2020 #1

我终于找到了答案:如果你一开始给你的观点一个框架并不重要。要使它们显示出来,您必须始终对屏幕上的所有视图进行高度限制。