UICollectionViewLayout和NavigationBar - 错误的y位置

时间:2018-01-31 09:14:57

标签: ios swift swift4

我发现这个很好的UICollectionViewLayout ExpandingCollectionView 问题是:只要我将一个导航栏添加到ViewController(带有搜索栏和scropebar),集合视图就会在下面滑动。

for test in soup.find_all('div', class_='rgt-col'):
  draft_kings = test.div.text
  salary_opp.append(draft_kings)

任何人都可以告诉我,我需要调整代码吗?我真的不知道,但我必须明天实施这个代码:O

非常感谢!

这是UICollectionViewController:

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)
    }
}

1 个答案:

答案 0 :(得分:0)

在布置def channel_wise_fc_layer(self, input, name): # bottom: (7x7x512) _, width, height, n_feat_map = input.get_shape().as_list() input_reshape = tf.reshape( input, [-1, width*height, n_feat_map] ) input_transpose = tf.transpose( input_reshape, [2,0,1] ) with tf.variable_scope(name): W = tf.get_variable( "W", shape=[n_feat_map,width*height, width*height], # (512,49,49) initializer=tf.random_normal_initializer(0., 0.005)) output = tf.batch_matmul(input_transpose, W) output_transpose = tf.transpose(output, [1,2,0]) output_reshape = tf.reshape( output_transpose, [-1, height, width, n_feat_map] ) return output_reshape 时,请使用safe area guides

collectionView

或许你可以使用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), ]) (如果你没有使用它),它应该隐含地处理它。