[英]Need help in understanding programmatically created UIView and UIViewController
[英]UIView of a UIViewController ambiguous position when created programmatically
我的UIViewController
出現黑屏。 我從調試器中了解到的原因是UIViewController
UIView
位置不明確。
我以編程方式創建了一個tagList
場景是這樣的:
我有一個UIStackView
。 在stackView
我添加了一個排列stackView
子視圖,它是一個UIImage
和一個UIViewController
。 在UIViewController
里面有一個UICollectionView
。 UIViewController
必須具有固定大小。 我給出了所有需要的約束:
初始化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()
}
結果是在我的層次結構中UIViewController
的UIView
沒有正確定位,所以我得到了一個黑屏。
我終於找到了答案 如果你一開始給你的觀點一個框架並不重要。 要使它們顯示出來,您必須始終對屏幕中的所有視圖進行高度限制。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.