【发布时间】:2020-03-31 08:53:42
【问题描述】:
我有以下布局:
UIImageView-
UIView(与UILabel) UITableView
随着tableView向上滚动,在实际滚动tableView之前,imageView的高度会降低。以下代码用于:
let headerImageViewMaxHeight: CGFloat = 200
let headerImageViewMinHeight: CGFloat = UIApplication.shared.statusBarFrame.height
@IBOutlet var headerImageViewHeightConstraint: NSLayoutConstraint!
@IBOutlet var headerImageView: UIImageView!
@IBOutlet var subtitleView: UIView!
@IBOutlet var subtitleLabel: TextLabel!
private var contentOffsetDictionary: NSMutableDictionary!
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.isKind(of: UICollectionView.self) {
let horizontalOffset: CGFloat = scrollView.contentOffset.x
let collectionView: UICollectionView = scrollView as! UICollectionView
contentOffsetDictionary.setValue(horizontalOffset, forKey: collectionView.tag.description)
} else if scrollView == tableView {
let y: CGFloat = scrollView.contentOffset.y
let newHeaderImageViewHeight: CGFloat = headerImageViewHeightConstraint.constant - y
if newHeaderImageViewHeight > headerImageViewMaxHeight {
headerImageViewHeightConstraint.constant = headerImageViewMaxHeight
} else if newHeaderImageViewHeight < headerImageViewMinHeight {
headerImageViewHeightConstraint.constant = headerImageViewMinHeight
} else {
headerImageViewHeightConstraint.constant = newHeaderImageViewHeight
scrollView.contentOffset.y = 0
}
}
}
以下两张图片显示了当前向下或向上滚动的状态:
向下滚动(初始状态)。
向上滚动。
如您所见,最初显示To Table -> Table 1 的灰色条向下滚动,大约为60 高。
当它向上滚动时,它会一直滚动到安全区域。我想要实现的是,一旦它到达安全区域,它会继续向上滚动进入 safeArea(一流),同时将文本保持在完全相同的位置(文本的 topSpace 到 safeArea 应该保持原样)。因此,基本上将UIView 增长到安全区域。
-
Header View可以忽略,因为它与 viewController 的顶部对齐,但除superView之外没有任何限制。 -
有问题的视图的
TopSpace是0 到UIImageView。
将headerImageViewMinHeight 更改为0,使其向上滚动到屏幕的实际顶部,而不展开它。所以这似乎是一个好的开始,但需要一些额外的逻辑或约束。
由于UIView 没有特定的高度限制,将其UILabel 的顶部空间从= 16 更改为=> 16,出现missing Y constraint or height 的警告。
否则,连同UILabel 的顶部空间到viewController 的=> 8 的safeArea 听起来它可以工作。
欢迎提出任何想法。
编辑:下图基本上显示了我想要的,只是这里UILabel的顶部没有与安全区域对齐。
这张图是通过将headerImageViewMinHeight改为0实现的。
【问题讨论】:
-
我认为你需要取消选中
Safe Area Relative Margins。 -
谢谢,我想这是一个好的开始。但这不是完整的解决方案,因为
UILabel与UIView的顶部和底部之间的间距仍然为16,从而防止UIView改变其高度。 -
对
once it reaches the safe area, it continues scrolling up into the safeArea (top notch), while keeping the text in exactly the same place (topSpace to safeArea of the text should stay the same). So, basically growing the UIView to go into the safeArea.有点困惑所以基本上,你想消除灰色视图(或文本?)和缺口之间的空间? -
你说得对,我的解释不是很清楚。我在消息底部添加了一张图片。所以,加上文本的顶部仍然与安全区域对齐(因此需要扩展 UIView 的高度。
标签: ios swift autolayout nslayoutconstraint iphone-x