gpt4 book ai didi

swift - 在 iPhone X 上填充页眉和页脚

转载 作者:行者123 更新时间:2023-11-28 07:50:52 25 4
gpt4 key购买 nike

我在 Swift 4 的 Collection View 上以编程方式处理页眉和页脚时遇到问题。这是我的应用程序在 iPhone X 上的图像:

Click here to see the image.

我想将页眉扩展到屏幕的最顶部(在缺口区域),将页脚扩展到屏幕的实际末端。我该怎么做?

最佳答案

来自 tylerf(Apple 员工,可能还有 @smileyborg)在 Apple Developer Forums 上:

If you are using flow layout in your collection view, you can use the section inset to make sure your content in the collection view remains inside the safe area. You can use the new UICollectionViewFlowLayout.sectionInsetReference property (set to .fromSafeArea) so this happens automatically.

但是,我的内容仍然滚动到我的页脚 View 并进入安全区域,如您的屏幕截图所示。相反,我是这样确定安全区域的:

self.safeAreaLayoutGuide = [UILayoutGuide new];
[self addLayoutGuide:safeAreaLayoutGuide];
[NSLayoutConstraint activateConstraints:@[
[safeAreaLayoutGuide.topAnchor constraintEqualToAnchor:self.topAnchor],
[safeAreaLayoutGuide.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],
[safeAreaLayoutGuide.bottomAnchor constraintEqualToAnchor:self.bottomAnchor],
[safeAreaLayoutGuide.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],
]];

然后,在我的 -layoutSubviews 方法中,我可以使用 safeAreaLayoutGuide.layoutFrame 插入我的 bounds:

UIEdgeInsets safeAreaEdgeInsets = 
UIEdgeInsetsMake(CGRectGetMinY(safeAreaLayoutGuide.layoutFrame),
CGRectGetMinX(safeAreaLayoutGuide.layoutFrame),
CGRectGetHeight(self.bounds) - CGRectGetMinY(safeAreaLayoutGuide.layoutFrame) - CGRectGetHeight(safeAreaLayoutGuide.layoutFrame),
CGRectGetWidth(self.bounds) - CGRectGetMinX(safeAreaLayoutGuide.layoutFrame) - CGRectGetWidth(safeAreaLayoutGuide.layoutFrame));
self.collectionView.frame = UIEdgeInsetsInsetRect(self.bounds, safeAreaEdgeInsets);

关于swift - 在 iPhone X 上填充页眉和页脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49740123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com