【问题标题】:UIView of a UIViewController ambiguous position when created programmatically以编程方式创建时 UIViewController 的 UIView 位置不明确
【发布时间】:2021-02-14 16:12:16
【问题描述】:

我的UIViewController 出现黑屏。我从调试器中了解到的原因是 UIViewUIViewController 中的位置不明确。

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

我有一个UIStackView。在stackView 中,我添加了排列好的子视图,即UIImageUIViewController。在UIViewController 里面有一个UICollectionViewUIViewController 必须具有固定大小。我给出了所有需要的约束:

初始化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()
}

结果是在我的层次结构中,UIViewControllerUIView 没有正确定位,所以我得到一个黑屏。

查看层次结构

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

【问题讨论】:

  • 试着简化一些事情......你的层次结构显示了一个带有图像视图和你的 TopBarViewController 的垂直堆栈视图,但你的屏幕截图以相反的顺序显示它们(顶部栏下方的图像)?从制作TopBarViewController 一个带有单个标签的普通视图控制器开始......你有白色背景吗?作为旁注...view.view 使阅读变得非常困难...调用视图控制器tagBarView 也令人困惑...它是UIView 吗?是UIViewController吗?

标签: ios swift iphone xcode uiviewcontroller


【解决方案1】:

我终于找到了答案,不管你最初是否给你的观点一个框架。要让它们显示出来,您必须始终对屏幕中的所有视图进行高度限制。

【讨论】:

    猜你喜欢
    • 2011-06-08
    • 1970-01-01
    • 2018-06-04
    • 2011-06-17
    • 2011-02-10
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    相关资源
    最近更新 更多