第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何設(shè)置單元格間距和UICollectionView-UICollectionViewFlow

如何設(shè)置單元格間距和UICollectionView-UICollectionViewFlow

拉丁的傳說 2019-12-09 11:22:41
我正在嘗試添加UICollectionView到ViewController,我需要“每行”有3個(gè)單元格,單元格之間沒有空格(它看起來應(yīng)該像網(wǎng)格)。單元格的寬度應(yīng)該是屏幕尺寸的三分之一,所以我認(rèn)為layout.item寬度應(yīng)該相同。但是然后我得到這個(gè):layout.itemSize width = screenSize.width / 3如果我減小該尺寸(例如減少7或8像素),效果會(huì)更好,但是行中的第三個(gè)單元格不是完全可見的,并且我仍然有該空白(頂部和底部以及左側(cè)和右側(cè))。在此處輸入圖片說明class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {    var collectionView: UICollectionView?    var screenSize: CGRect!    var screenWidth: CGFloat!    var screenHeight: CGFloat!    override func viewDidLoad() {        super.viewDidLoad()        screenSize = UIScreen.mainScreen().bounds        screenWidth = screenSize.width        screenHeight = screenSize.height        // Do any additional setup after loading the view, typically from a nib        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()        layout.sectionInset = UIEdgeInsets(top: 20, left: 0, bottom: 10, right: 0)        layout.itemSize = CGSize(width: screenWidth / 3, height: screenWidth / 3)        collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)        collectionView!.dataSource = self        collectionView!.delegate = self        collectionView!.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")        collectionView!.backgroundColor = UIColor.greenColor()        self.view.addSubview(collectionView!)    }    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {        return 1    }
查看完整描述

3 回答

?
揚(yáng)帆大魚

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超9個(gè)贊

添加這2行


layout.minimumInteritemSpacing = 0

layout.minimumLineSpacing = 0

所以你有了:


   // Do any additional setup after loading the view, typically from a nib.

        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()

        layout.sectionInset = UIEdgeInsets(top: 20, left: 0, bottom: 10, right: 0)

        layout.itemSize = CGSize(width: screenWidth/3, height: screenWidth/3)

        layout.minimumInteritemSpacing = 0

        layout.minimumLineSpacing = 0

        collectionView!.collectionViewLayout = layout

這將刪除所有空格并為您提供網(wǎng)格布局:


在此處輸入圖片說明


如果希望第一列的寬度等于屏幕寬度,請(qǐng)?zhí)砑右韵鹿δ埽?/p>


func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

    if indexPath.row == 0

    {

        return CGSize(width: screenWidth, height: screenWidth/3)

    }

    return CGSize(width: screenWidth/3, height: screenWidth/3);


}

網(wǎng)格布局現(xiàn)在看起來像(我還在第一個(gè)單元格中添加了藍(lán)色背景)


查看完整回答
反對(duì) 回復(fù) 2019-12-09
?
哆啦的時(shí)光機(jī)

TA貢獻(xiàn)1779條經(jīng)驗(yàn) 獲得超6個(gè)贊

對(duì)于Swift 3和XCode 8來說,這可行。請(qǐng)按照以下步驟來實(shí)現(xiàn):


{

    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()

    let width = UIScreen.main.bounds.width

    layout.sectionInset = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)

    layout.itemSize = CGSize(width: width / 2, height: width / 2)

    layout.minimumInteritemSpacing = 0

    layout.minimumLineSpacing = 0

    collectionView!.collectionViewLayout = layout

}

將此代碼放入viewDidLoad()函數(shù)。


查看完整回答
反對(duì) 回復(fù) 2019-12-09
?
肥皂起泡泡

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超6個(gè)贊

在某些情況下,設(shè)置UICollectionViewFlowLayoutin viewDidLoad或ViewWillAppear不影響collectionView。


設(shè)置UICollectionViewFlowLayoutin viewDidAppear可能會(huì)導(dǎo)致在運(yùn)行時(shí)看到像元大小的變化。


Swift 3中的另一個(gè)解決方案:


extension YourViewController : UICollectionViewDelegateFlowLayout{


    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

        return UIEdgeInsets(top: 20, left: 0, bottom: 10, right: 0)

    }


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

        let collectionViewWidth = collectionView.bounds.width

        return CGSize(width: collectionViewWidth/3, height: collectionViewWidth/3)

    }


    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {

        return 0

    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {

        return 20

    }

}


查看完整回答
反對(duì) 回復(fù) 2019-12-09
  • 3 回答
  • 0 關(guān)注
  • 535 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)