简体   繁体   English

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

[英]UIView of a UIViewController ambiguous position when created programmatically

I get a black screen for my UIViewController .我的UIViewController出现黑屏。 The reason as I understood from the debugger is ambiguous position for UIView inside the UIViewController .我从调试器中了解到的原因是UIViewController UIView位置不明确。

I have created a tagList programmatically the scenario is like this:我以编程方式创建了一个tagList场景是这样的:

I have a UIStackView .我有一个UIStackView Inside the stackView I add the arranged subview which is an UIImage and a UIViewController .stackView我添加了一个排列stackView子视图,它是一个UIImage和一个UIViewController Inside the UIViewController there is a UICollectionView .UIViewController里面有一个UICollectionView The UIViewController must have a fixed size. UIViewController必须具有固定大小。 I gave all the needed constraints :我给出了所有需要的约束:

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

calling the tagList调用tagList

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

constraints of the UIStackView 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

Then again I created the collectionView inside my viewController programmatically this is the setup for the collectionView and FlowLayout然后我再次以编程方式在我的 viewController 中创建了 collectionView 这是 collectionView 和 FlowLayout 的设置

setup for the collectionView and 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
    
}

The constraints for the UICollectioView 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()
}

The result is that in my hierarchy the UIView of the UIViewController is not properly positioned so I get a black screen.结果是在我的层次结构中UIViewControllerUIView没有正确定位,所以我得到了一个黑屏。

View Hierarchy查看层次结构查看层级

The Final Result The view is the black one on the top(which should be white)最终结果视图是顶部的黑色(应该是白色的) 在此处输入图片说明

I finally found the answer it doesn't matter if you at first give your views a frame.我终于找到了答案 如果你一开始给你的观点一个框架并不重要。 to make them show up you have to always have a height constraint on all the view in your screen.要使它们显示出来,您必须始终对屏幕中的所有视图进行高度限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在理解以编程方式创建的UIView和UIViewController时需要帮助 - Need help in understanding programmatically created UIView and UIViewController 如何添加UIViewController作为在以编程方式创建的UIView中创建的UIButton操作的目标? - How to add UIViewController as target of UIButton action created in programmatically created UIView? 以编程方式将 UIView 连接到 UIViewController? - Attach a UIView to a UIViewController programmatically? 通过UIViewController以编程方式实现UIView - Implementing programmatically UIView over a UIViewController 以编程方式向UIView或UIViewController添加约束? - Programmatically add constraints to UIView or UIViewController? 子类化UIView的约束抛出以编程方式创建的UILabel位置 - Constraints on subclassed UIView throw off programmatically created UILabel position 获取UIViewController以编程方式重新加载不同的UIView - Get UIViewController to reload different UIView programmatically 如何在UIViewController中访问由UIView的子类创建的对象 - How to access an object created by a subclass of UIView in a UIViewController 当我从基类中以编程方式替换UIViewController的根UIView时,Outlet变为nil - My Outlets get nil when I replace root UIView of UIViewController programmatically from a base class 将动画添加到以编程方式创建的UIview - add animation to UIview created programmatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM