Swift-可伸缩标头在iOS 13下不再起作用

时间:2019-10-05 12:12:37

标签: ios swift

我做了一个像下面这样的可伸缩标头。

enter image description here

这是我的表格视图后面的imageView。那是我的代码。

func scrollViewDidScroll(_ scrollView: UIScrollView) {

    let y = 300 - (scrollView.contentOffset.y + 170)
    let height = min(max(y, 220), 400)
    backdropPictureImageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width , height: height)
}

在iOS 12下,它可以完美运行,但在iOS 13下不再可用。发生了什么变化?

2 个答案:

答案 0 :(得分:0)

对于iOS 13,这对我有用:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.


    backdropPictureImageView = UIImageView(image: UIImage(named: "pic"))
    backdropPictureImageView?.contentMode = .scaleAspectFill
    self.view.insertSubview(backdropPictureImageView!, at: 0)
    self.view.frame = tableView.bounds
}

override func scrollViewDidScroll(_ scrollView: UIScrollView) {

    let y = 300 - (scrollView.contentOffset.y + 170)
    let height = min(max(y, 220), 400)
    backdropPictureImageView!.frame = CGRect(x: 0, y: scrollView.contentOffset.y, width: UIScreen.main.bounds.size.width , height: height)
}

答案 1 :(得分:0)

它将通过自定义UICollectionViewFlowLayout与集合视图一起使用

class StretchyHeaderLayout: UICollectionViewFlowLayout {

        override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {

            let layoutAttributes = super.layoutAttributesForElements(in: rect)! as [UICollectionViewLayoutAttributes]
            let contentOffset = collectionView!.contentOffset

            if (contentOffset.y < 0) {
                let deltaY = abs(contentOffset.y)
                for attributes in layoutAttributes where  attributes.representedElementKind == UICollectionView.elementKindSectionHeader {
                    var frame = attributes.frame
                        frame.size.height =  headerReferenceSize.height + deltaY
                        frame.origin.y = frame.minY - deltaY
                        attributes.frame = frame
                }
            }

            return layoutAttributes
        }

        override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
            return true
        }

    }

不要忘记在ViewDidLoad中更改collectionView的布局

let layout =  StretchyHeaderLayout()
yourCollectionView.collectionViewLayout = layout