gpt4 book ai didi

ios - UICollectionViewLayout 和 NavigationBar - y 位置错误

转载 作者:行者123 更新时间:2023-11-30 11:48:47 25 4
gpt4 key购买 nike

我发现了这个不错的 UICollectionViewLayout ExpandingCollectionView问题是:一旦我向 ViewController 添加导航栏(带有搜索栏和缩放栏), Collection View 就会滑到下面。

import Foundation
import UIKit

/* The heights are declared as constants outside of the class so they can be easily referenced elsewhere */
struct UltravisualLayoutConstants {
struct Cell {
/* The height of the non-featured cell */
static let standardHeight: CGFloat = 100
/* The height of the first visible cell */
static let featuredHeight: CGFloat = 280
}
}

class UltravisualLayout:UICollectionViewLayout{

// MARK: Properties and Variables

/* The amount the user needs to scroll before the featured cell changes */
let dragOffset: CGFloat = 180.0

var cache = [UICollectionViewLayoutAttributes]()

/* Returns the item index of the currently featured cell */
var featuredItemIndex: Int {
get {
/* Use max to make sure the featureItemIndex is never < 0 */
return max(0, Int(collectionView!.contentOffset.y / dragOffset))
}
}

/* Returns a value between 0 and 1 that represents how close the next cell is to becoming the featured cell */
var nextItemPercentageOffset: CGFloat {
get {
return (collectionView!.contentOffset.y / dragOffset) - CGFloat(featuredItemIndex)
}
}

/* Returns the width of the collection view */
var width: CGFloat {
get {
return collectionView!.bounds.width
}
}

/* Returns the height of the collection view */
var height: CGFloat {
get {
return collectionView!.bounds.height
}
}

/* Returns the number of items in the collection view */
var numberOfItems: Int {
get {
return collectionView!.numberOfItems(inSection: 0)
}
}

// MARK: UICollectionViewLayout

/* Return the size of all the content in the collection view */

override var collectionViewContentSize: CGSize{
let contentHeight = (CGFloat(numberOfItems) * dragOffset) + (height - dragOffset)
return CGSize(width: width, height: contentHeight)
}

override func prepare() {
cache.removeAll(keepingCapacity: false)
let standardHeight = UltravisualLayoutConstants.Cell.standardHeight
let featuredHeight = UltravisualLayoutConstants.Cell.featuredHeight

var frame = CGRect.zero
var y: CGFloat = 0

for item in 0..<numberOfItems {
// 1
let indexPath = IndexPath(item:item, section:0)
let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)

// 2
attributes.zIndex = item
var height = standardHeight

// 3
if indexPath.item == featuredItemIndex {
// 4
let yOffset = standardHeight * nextItemPercentageOffset
y = collectionView!.contentOffset.y - yOffset
height = featuredHeight
} else if indexPath.item == (featuredItemIndex + 1) && indexPath.item != numberOfItems {
// 5
let maxY = y + standardHeight
height = standardHeight + max((featuredHeight - standardHeight) * nextItemPercentageOffset, 0)
y = maxY - height
}

// 6
frame = CGRect(x: 0, y: y, width: width, height: height)
attributes.frame = frame
cache.append(attributes)
y = frame.maxY
}
}

/* Return all attributes in the cache whose frame intersects with the rect passed to the method */
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var layoutAttributes = [UICollectionViewLayoutAttributes]()
for attributes in cache {
if attributes.frame.intersects(rect) {
layoutAttributes.append(attributes)
}
}
return layoutAttributes
}
/* Return true so that the layout is continuously invalidated as the user scrolls */
override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
return true
}

override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
let itemIndex = round(proposedContentOffset.y / dragOffset)
let yOffset = itemIndex * dragOffset
return CGPoint(x: 0, y: yOffset)
}
}

谁能告诉我,我需要在哪里调整代码?我真的不知道,但我必须在明天之前实现此代码:O

非常感谢!

这是 UICollectionViewController:

import UIKit

private let reuseIdentifier = "Cell"

class InspirationsViewController: UICollectionViewController {

let inspirations = Inspiration.allInspirations()


override func viewDidLoad() {
super.viewDidLoad()

if let patternImage = UIImage(named: "Pattern") {
view.backgroundColor = UIColor(patternImage: patternImage)
}
collectionView!.backgroundColor = UIColor.clear
collectionView!.decelerationRate = UIScrollViewDecelerationRateFast

let searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.scopeButtonTitles = ["All", "Chocolate", "Hard", "Other"]
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = true
definesPresentationContext = true

self.definesPresentationContext = true
}

}

extension InspirationsViewController {

override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return inspirations.count
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "InspirationCell", for: indexPath) as! InspirationCell
cell.inspiration = inspirations[indexPath.item]
return cell
}

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let layout = collectionViewLayout as! UltravisualLayout
let offset = layout.dragOffset * CGFloat(indexPath.item)
if collectionView.contentOffset.y != offset {
collectionView.setContentOffset(CGPoint(x: 0, y: offset), animated: true)
}
}
}

最佳答案

布局collectionView时,使用safe area guides :

NSLayoutConstraint.activate([
collectionView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
collectionView.leftAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leftAnchor),
collectionView.rightAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.rightAnchor),
collectionView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor),
])

或者也许您可以使用UICollectionViewController(如果您没有使用它),它应该隐式地处理它。

关于ios - UICollectionViewLayout 和 NavigationBar - y 位置错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48538437/

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