2017-02-09 71 views
0

我想要的PageControl添加到我的收藏查看這是我的CollectionView細胞裏面,所以我需要聲明UIPageControl所以如何聲明UIPageControl內

let CollectionViewPageControl: UIPageControl = { 
    let pageControl = UIPageControl() 
    pageControl.frame = CGRect(x: 0, y: 100, width: 10, height: 10) 
    pageControl.currentPage = 0 
    pageControl.currentPageIndicatorTintColor = UIColor.red 
    pageControl.pageIndicatorTintColor = UIColor.lightGray 
    return pageControl 
} 

,但我得到這個錯誤Cannot convert value of type '() -> _' to specified type 'UIPageControl'

有幫助嗎?

回答

0

您聲明collectionViewPageControlUIPageControl,但您正在爲其分配閉包。

你可以這樣做:

let collectionViewPageControl: UIPageControl = UIPageControl() 
collectionViewPageControl.frame = CGRect(x: 0, y: 100, width: 10, height: 10) 
collectionViewPageControl.currentPage = 0 
collectionViewPageControl.currentPageIndicatorTintColor = UIColor.red 
collectionViewPageControl.pageIndicatorTintColor = UIColor.lightGray 

您可以在斯威夫特here閱讀更多有關閉。

不要忘記將它添加到您的collectionViewCell(或任何將作爲分頁的UICollectionView的父母)。

yourCollectionViewCell.addSubview(collectionViewPageControl) 
+0

不要忘記'self.addSubview(collectionViewPageControl)'。 –

+0

當然,我只是推斷OP在UIPageControl的聲明上只有問題,因爲這是他發佈的唯一代碼。但我會編輯答案:) –