不是集合視圖單元格,我想通過在我的ViewController中應用特定的邊框函數(如borderColor和cornerRadius方法)。我說的是一個邊境封裝細胞的整體來看,有點像這樣:如何在Swift中將邊框添加到集合視圖中?
大加讚賞。
不是集合視圖單元格,我想通過在我的ViewController中應用特定的邊框函數(如borderColor和cornerRadius方法)。我說的是一個邊境封裝細胞的整體來看,有點像這樣:如何在Swift中將邊框添加到集合視圖中?
大加讚賞。
您需要將邊框添加到collectionView視圖本身。這是封裝細胞。你可以做到這一點無論是在viewDidLayoutSubviews:
或viewDidLoad:
像這樣:
collectionView.layer.borderWidth = 2.0
collectionView.layer.cornerRadius = 5.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)
}