2016-11-01 58 views
0

我一直在編程的應用程序像Instagram的學習迅捷和ios編程。現在我創建了UIStackView,這將是一個頂級欄,以及3個底部按鈕,用於發佈請求和更改圖像。收藏查看位置,如Instagram的應用程序swift

我有問題 - 我無法在CollectionView上設置位置,寬度和刪除單元格間距。我想: 的CollectionView上的所有設備全寬度圖像= 1 的CollectionView應位於頂部欄和底部按鍵全高

什麼我現在之間 單元格間距?

屏幕我的模擬器:http://prntscr.com/d1mmu1

屏幕我的故事板:http://prntscr.com/d1mnhr

我已經試過這樣做有故事板,改變了所有的參數應用,但處理不當的達到自己的目標。 如果我將相同的高度添加到應用程序的CollectionView和主視圖,它會佔用所有屏幕,並在滾動後消失... 如何設置該位置和寬度,高度和單元格間距?

here is a code of my viewcontroller for CollectionView - 

// MARK: - UICollectionViewDataSource協議

// tell the collection view how many cells to make 
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return self.items.count 
} 

// make a cell for each cell index path 
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    // get a reference to our storyboard cell 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell 

    // Use the outlet in our custom class to get a reference to the UILabel in the cell 
    cell.imageView.image = UIImage(named: "main/p\(self.items[indexPath.item].code)/main/main.jpg") 
    print("main_card_images/p\(self.items[indexPath.item].code)/main/main") 
    return cell 
} 

// MARK: - UICollectionViewDelegate protocol 

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    // handle tap events 
    print("You selected cell #\(indexPath.item)!") 
    print(self.items[indexPath.item]) 
} 

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    let yourNextViewController = (segue.destination as! ViewControllerCard) 
    let indexPath = collectionView.indexPath(for: sender as! UICollectionViewCell) 
    yourNextViewController.mainCardImage = self.items[(indexPath?.item)!] 
} 
+0

檢查我的答案http://stackoverflow.com/questions/40325327/adjust-cellsize-for-fit-all-screens/40325452#4 0325452 – Joe

回答

1

嘗試此代碼。

注:下面的代碼適用於所有的iPhone models.If測試需要不同數量的column.You的需要與細胞的寬度和高度玩或看我其他的答案只有4列布局Adjust cellsize for fit all screens?

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize { 
    let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout 
    layout.sectionInset = UIEdgeInsets(top: 3, left: 3, bottom: 3, right: 3) 
    layout.minimumInteritemSpacing = 03 
    layout.minimumLineSpacing = 03 
    layout.invalidateLayout() 

    return CGSize(width: ((self.view.frame.width/4) - 4), height:((self.view.frame.width/4) - 4)); 
} 

輸出:

enter image description here