2016-08-25 1 views
0

안녕하세요. uicollectionview 사용자 정의 레이아웃을 사용하고 있는데 bottom paddingscrolling sizes에 대해 issues이 있습니다. picture 아래에서 볼 수 있습니다. 스크롤 할 때UICollectionView 아래쪽 패딩 및 스크롤링 크기를 설정하는 방법

또한 상단면 첫 번째 셀은 항상 맨 위에 유지 (항상 지금 아무 문제 유지되어야합니다 왼쪽 셀을하지만, 스크롤 할 때 위쪽 셀은 스크롤해야 함)

enter image description here

내가 코드 아래에서 viewDidLoad에 설정하려고

하지만 dont 작업

self.automaticallyAdjustsScrollViewInsets = false 
self.collectionView.contentInset = UIEdgeInsetsMake(20, 20, 120, 100); 

내 사용자 정의 collectionview layout 파일

import UIKit 

public var CELL_HEIGHT = CGFloat(22) 

protocol CustomCollectionViewDelegateLayout: NSObjectProtocol { 

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, widthForItemAtIndexPath indexPath: NSIndexPath) -> CGFloat 
} 

class CustomCollectionViewLayout: UICollectionViewLayout { 

    // Used for calculating each cells CGRect on screen. 
    // CGRect will define the Origin and Size of the cell. 

    let STATUS_BAR = UIApplication.sharedApplication().statusBarFrame.height 

    // Dictionary to hold the UICollectionViewLayoutAttributes for 
    // each cell. The layout attribtues will define the cell's size 
    // and position (x, y, and z index). I have found this process 
    // to be one of the heavier parts of the layout. I recommend 
    // holding onto this data after it has been calculated in either 
    // a dictionary or data store of some kind for a smooth performance. 
    var cellAttrsDictionary = Dictionary<NSIndexPath, UICollectionViewLayoutAttributes>() 

    // Defines the size of the area the user can move around in 
    // within the collection view. 
    var contentSize = CGSize.zero 

    // Used to determine if a data source update has occured. 
    // Note: The data source would be responsible for updating 
    // this value if an update was performed. 
    var dataSourceDidUpdate = true 

    weak var delegate: CustomCollectionViewDelegateLayout? 

    override func collectionViewContentSize() -> CGSize { 
     return self.contentSize 
    } 

    override func prepareLayout() { 

     // Only update header cells. 
     if !dataSourceDidUpdate { 

      // Determine current content offsets. 
      let xOffset = collectionView!.contentOffset.x 
      let yOffset = collectionView!.contentOffset.y 

      if collectionView?.numberOfSections() > 0 { 
       for section in 0...collectionView!.numberOfSections()-1 { 

        // Confirm the section has items. 
        if collectionView?.numberOfItemsInSection(section) > 0 { 

         // Update all items in the first row. 
         if section == 0 { 
          for item in 0...collectionView!.numberOfItemsInSection(section)-1 { 

           // Build indexPath to get attributes from dictionary. 
           let indexPath = NSIndexPath(forItem: item, inSection: section) 

           // Update y-position to follow user. 
           if let attrs = cellAttrsDictionary[indexPath] { 
            var frame = attrs.frame 

            // Also update x-position for corner cell. 
            if item == 0 { 
             frame.origin.x = xOffset 
            } 

            frame.origin.y = yOffset 
            attrs.frame = frame 
           } 

          } 

          // For all other sections, we only need to update 
          // the x-position for the fist item. 
         } else { 

          // Build indexPath to get attributes from dictionary. 
          let indexPath = NSIndexPath(forItem: 0, inSection: section) 

          // Update y-position to follow user. 
          if let attrs = cellAttrsDictionary[indexPath] { 
           var frame = attrs.frame 
           frame.origin.x = xOffset 
           attrs.frame = frame 
          } 

         } 
        } 
       } 
      } 


      // Do not run attribute generation code 
      // unless data source has been updated. 
      return 
     } 

     // Acknowledge data source change, and disable for next time. 
     dataSourceDidUpdate = false 

     var maxWidthInASection = CGFloat(0) 

     // Cycle through each section of the data source. 
     if collectionView?.numberOfSections() > 0 { 
      for section in 0...collectionView!.numberOfSections()-1 { 

       // Cycle through each item in the section. 
       if collectionView?.numberOfItemsInSection(section) > 0 { 

        var prevCellAttributes: UICollectionViewLayoutAttributes? 

        for item in 0...collectionView!.numberOfItemsInSection(section)-1 { 

         let cellIndex = NSIndexPath(forItem: item, inSection: section) 

         guard let width = delegate?.collectionView(collectionView!, layout: self, widthForItemAtIndexPath: cellIndex) else { 
          print("Please comform to CustomCollectionViewDelegateLayout protocol") 
          return 
         } 

         // Build the UICollectionVieLayoutAttributes for the cell. 

         var xPos = CGFloat(0) 
         let yPos = CGFloat(section) * CELL_HEIGHT 

         if let prevCellAttributes = prevCellAttributes { 
          xPos = CGRectGetMaxX(prevCellAttributes.frame) 
         } 

         let cellAttributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: cellIndex) 
         cellAttributes.frame = CGRect(x: xPos, y: yPos, width: width, height: CELL_HEIGHT) 

         // Determine zIndex based on cell type. 
         if section == 0 && item == 0 { 
          cellAttributes.zIndex = 4 
         } else if section == 0 { 
          cellAttributes.zIndex = 3 
         } else if item == 0 { 
          cellAttributes.zIndex = 2 
         } else { 
          cellAttributes.zIndex = 1 
         } 

         // Save the attributes. 
         cellAttrsDictionary[cellIndex] = cellAttributes 
         prevCellAttributes = cellAttributes 

         let maxX = CGRectGetMaxX(cellAttributes.frame) 

         if maxWidthInASection < maxX { 
          maxWidthInASection = maxX 
         } 
        } 
       } 

      } 
     } 

     // Update content size. 
     let contentWidth = maxWidthInASection 
     let contentHeight = Double(collectionView!.numberOfSections()) 
     self.contentSize = CGSize(width: Double(contentWidth), height: contentHeight) 
     self.contentSize.height = CGFloat(Double(collectionView!.numberOfSections())) 


    } 

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

     // Create an array to hold all elements found in our current view. 
     var attributesInRect = [UICollectionViewLayoutAttributes]() 

     // Check each element to see if it should be returned. 
     for cellAttributes in cellAttrsDictionary.values.elements { 
      if CGRectIntersectsRect(rect, cellAttributes.frame) { 
       attributesInRect.append(cellAttributes) 
      } 
     } 

     // Return list of elements. 
     return attributesInRect 
    } 

    override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? { 
     return cellAttrsDictionary[indexPath]! 
    } 

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

} 

내가 storyboard에 변화 collecionview 속성을 시도하지만 나던 작품은 내가

enter image description here

이 또한 내가 줌 4 만 줌을 선택 아래 현재 collectionview 개체 속성 아래에 사진 첨부 기능이 작동하지 않습니까? 왜 ? 그것은 또한 일해야한다 줌 기능.

+0

아이디어가 있으십니까? 이 문제를 어떻게 해결할 수 있습니까? – SwiftDeveloper

+0

한 번보기 http://stackoverflow.com/questions/21067091/uicollectionview-not-able-to-scroll-to-see-the-entire-last-row –

+0

@ Anbu.Karthik 형제는 내가 본 것이지만 그것을 해결하지 못했습니다. 내 코드를 고칠 수 있습니까? 너는 어떤 생각을 가지고 있니? – SwiftDeveloper

답변

3

답변하기에는 너무 늦었지만 다른 사용자에게 유용 할 수 있습니다.

override func viewDidLayoutSubviews() { 
    super.viewDidLayoutSubviews() 
    collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 50, right: 0) 
}