カスタムキーボードでUICollectionViewを使いたいが表示されない
テスト的に書いたソースです。
表示もされません。どこが悪いのか教えていただけないでしょうか。エラーは出ませんが表示も出ません。
よろしくお願いいたします。
PS:最低限のソースに修正しました。
import UIKit
class KeyboardViewController: UIInputViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet var nextKeyboardButton: UIButton!
private var lexicon: UILexicon!
var myCollectionView : UICollectionView!
override func updateViewConstraints() {
super.updateViewConstraints()
// Add custom view sizing constraints here
}
override func viewDidLoad() {
super.viewDidLoad()
// Perform custom UI setup here
self.nextKeyboardButton = UIButton(type: .System)
self.nextKeyboardButton.setTitle(NSLocalizedString("Next Keyboard", comment: "Title for 'Next Keyboard' button"), forState: .Normal)
self.nextKeyboardButton.sizeToFit()
self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = false
self.nextKeyboardButton.addTarget(self, action: "advanceToNextInputMode", forControlEvents: .TouchUpInside)
self.view.addSubview(self.nextKeyboardButton)
let nextKeyboardButtonLeftSideConstraint = NSLayoutConstraint(item: self.nextKeyboardButton, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1.0, constant: 0.0)
let nextKeyboardButtonBottomConstraint = NSLayoutConstraint(item: self.nextKeyboardButton, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1.0, constant: 0.0)
self.view.addConstraints([nextKeyboardButtonLeftSideConstraint, nextKeyboardButtonBottomConstraint])
// CollectionViewのレイアウトを生成.
let layout = UICollectionViewFlowLayout()
// サイズの設定
layout.itemSize.height = 20
layout.itemSize.width = 100
// Cell一つ一つの大きさ.
layout.itemSize = CGSizeMake(10, 10)
// Cellのマージン.
layout.sectionInset = UIEdgeInsetsMake(16, 16, 32, 16)
// セクション毎のヘッダーサイズ.
layout.headerReferenceSize = CGSizeMake(100,30)
// CollectionViewを生成.
myCollectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
myCollectionView.frame.size.height = 100
myCollectionView.frame.size.width = 100
myCollectionView.frame.offset(dx: 100, dy: 100)
myCollectionView.delegate = self
print(myCollectionView.delegate)
myCollectionView.dataSource = self
print(myCollectionView.dataSource)
// Cellに使われるクラスを登録.
myCollectionView.registerClass(CustomUICollectionViewCell.self, forCellWithReuseIdentifier: "MyCell")
myCollectionView.sizeToFit()
print(myCollectionView.frame)
view.addSubview(myCollectionView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated
}
override func textWillChange(textInput: UITextInput?) {
// The app is about to change the document's contents. Perform any preparation here.
}
override func textDidChange(textInput: UITextInput?) {
// The app has just changed the document's contents, the document context has been updated.
var textColor: UIColor
let proxy = self.textDocumentProxy
if proxy.keyboardAppearance == UIKeyboardAppearance.Dark {
textColor = UIColor.whiteColor()
} else {
textColor = UIColor.blackColor()
}
self.nextKeyboardButton.setTitleColor(textColor, forState: .Normal)
}
// MARK: - UITableViewDataSource
/*
Cellが選択された際に呼び出される
*/
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
print("Num: \(indexPath.row)")
}
/*
Cellの総数を返す
*/
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
/*
Cellに値を設定する
*/
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell : CustomUICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as! CustomUICollectionViewCell
cell.textLabel?.text = indexPath.row.description
return cell
}
}
class CustomUICollectionViewCell : UICollectionViewCell{
var textLabel : UILabel?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
// UILabelを生成.
textLabel = UILabel(frame: CGRectMake(0, 0, frame.width, frame.height))
textLabel?.text = "nil"
textLabel?.backgroundColor = UIColor.whiteColor()
textLabel?.textAlignment = NSTextAlignment.Center
// Cellに追加.
self.contentView.addSubview(textLabel!)
}
}