我在视图中添加了UICollectionView,而不是滚动。
private func addFriendCollectionView() {
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 90, height: 50)
let positionRectangle = CGRect(x: 0, y: 100, width: (mainContainerView?.frame.width)!, height: 100)
friendsCollection = UICollectionView(frame: positionRectangle, collectionViewLayout: layout)
friendsCollection?.registerClass(FriendCollectionViewCell.self, forCellWithReuseIdentifier: "friendCell")
friendsCollection?.backgroundColor = UIColor.whiteColor()
friendsCollection?.dataSource = self
friendsCollection?.delegate = self
self.mainContainerView?.addSubview(friendsCollection!)
}
但如果我改变
private func addFriendCollectionView() {
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 90, height: 50)
let positionRectangle = CGRect(x: 0, y: 100, width: (mainContainerView?.frame.width)!, height: 100)
friendsCollection = UICollectionView(frame: positionRectangle, collectionViewLayout: layout)
friendsCollection?.registerClass(FriendCollectionViewCell.self, forCellWithReuseIdentifier: "friendCell")
friendsCollection?.backgroundColor = UIColor.whiteColor()
friendsCollection?.dataSource = self
friendsCollection?.delegate = self
// Add collection view to root view
self.view.addSubview(friendsCollection!)
}
一切正常。
如何向UICollectionView添加滚动?按照代码显示如何容器视图
private func addBackGroundView() {
let imageRect = CGRect(x: 5, y: 70, width: self.view.frame.width - 10, height: self.view.frame.height - 75)
backGroundImage = InviteFriendsBackGroundImageView(frame: imageRect)
backGroundImage!.image = InviteFriendsData.sharedInstance.image
self.view.addSubview(backGroundImage!)
}
private func addMainContainerView() {
let mainContainerPosition = CGRect(x: 0, y: 50, width: self.view.frame.width - 10, height: self.view.frame.height - 80)
mainContainerView = UIView(frame: mainContainerPosition)
mainContainerView?.backgroundColor = UIColor.whiteColor()
backGroundImage?.addSubview(mainContainerView!)
}