2017-06-08 130 views

回答

1
在您的viewDidLoad功能

,只需添加這種類型的線(選擇你的價值)。

collectionView.layer.borderColor = UIColor.green.cgColor 
collectionView.layer.borderWidth = 3.0 
collectionView.layer.cornerRadius = 3.0//if you want corner radius.addtional 

enter image description here

1

您需要將邊框添加到collectionView視圖本身。這是封裝細胞。你可以做到這一點無論是在viewDidLayoutSubviews:viewDidLoad:

像這樣:

collectionView.layer.borderWidth = 2.0 
collectionView.layer.cornerRadius = 5.0 
0

你可以做,使用UICollectionViewDelegateFlowLayout和這裏的如何。

// your margin for top, botom, right, left 
let inBetweenMargin: CGFloat = 30 

let collectionViewSize: CGSize = { 

    // define the max width of your collection view 
    let screen = UIScreen.main.bounds.size.width 

    // your margin for top, botom, right, left 
    let inBetweenMargin: CGFloat = 30 

    // left, mind, right 'inBetweenMargin * 3' 
    let cellSquareSize = screen - (inBetweenMargin * 3); 

    return CGSize(width: cellSquareSize, height: cellSquareSize) 
}() 

// MARK: - UICollectionViewDelegateFlowLayout 

// computed cell size 
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

    return collectionViewSize 
} 

// for spacing in between cells 
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 

    return inBetweenMargin 
} 

// for spacing in between cells 
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { 

    return inBetweenMargin 
} 

// we define inset for us to achieved an equal margin for top, botom, right, left 
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { 

    return UIEdgeInsets(top: inBetweenMargin, left: inBetweenMargin, bottom: inBetweenMargin, right: inBetweenMargin) 
} 
相關問題