我有一个UIViewController
,其中包含一个用于垂直分页的UICollectionView
。 UICollectionView
(图片中的蓝色区域)固定在超级视图的顶部,而UICollectionViewCell
(黄色)则不固定。我试图将UICollectionViewCell
的顶部约束明确地固定在UICollectionView
的顶部,但是程序崩溃了。
class HomeViewController: UIViewController, UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {
super.viewDidLoad()
let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout())
collectionView.register(FeedCell.self, forCellWithReuseIdentifier: reuseIdentifier)
collectionView.backgroundColor = .blue
collectionView.showsVerticalScrollIndicator = false
collectionView.isPagingEnabled = true
view.addSubview(collectionView)
collectionView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
collectionView.topAnchor.constraint(equalTo: view.topAnchor),
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
view.bottomAnchor.constraint(equalTo: collectionView.bottomAnchor),
view.trailingAnchor.constraint(equalTo: collectionView.trailingAnchor)
])
collectionView.dataSource = self
collectionView.delegate = self
}
override var preferredStatusBarStyle: UIStatusBarStyle {
.lightContent
}
// MARK: UICollectionViewDelegateFlowLayout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: view.frame.width, height: collectionView.bounds.height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
}
extension HomeViewController: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
4
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
cell.backgroundColor = indexPath.item % 2 == 0 ? .yellow : .orange
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}
}
答案 0 :(得分:0)
与其使用import tkinter as tk
from tkinter import simpledialog
ROOT = tk.Tk()
ROOT.withdraw()
# the input dialog
USER_INP = simpledialog.askstring(title="Test",
prompt="What's your Name?:")
# check it out
print("Hello", USER_INP)
固定到所有安全区域边缘,不如将集合视图的顶部约束固定到pinEdgesToSafeArea(to:)
的顶部约束。我不知道view
的来源,因此无法评论其替代品,但是您可以使用它将收藏夹视图固定在屏幕顶部:
pinEdgesToSafeArea(to:)
编辑:
根据您发布的说明,尝试将collectionView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
设置为automaticallyAdjustsScrollIndicatorInsets
:
false
答案 1 :(得分:0)
只需将您的收藏夹视图固定在View的顶部,而不是safeArea
collectionView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true;
并仅将您的收藏夹视图用于安全区域插图:
collectionView.contentInsetAdjustmentBehavior = .never;