UICollectionView无法在自定义键盘扩展中使用

时间:2016-05-21 09:23:11

标签: ios objective-c swift uicollectionview custom-keyboard

我正在尝试在自定义键盘扩展中实现UICollectionView但它无法正常工作。 这就是我在viewDidLoad方法中所做的。

    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
    layout.itemSize = CGSize(width: 90, height: 120)

    collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
    collectionView.dataSource = self
    collectionView.delegate = self
    collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
    collectionView.backgroundColor = UIColor.whiteColor()

    self.view.addSubview(collectionView)

我已将UICollectionViewDelegateFlowLayoutUICollectionViewDataSource协议添加到我的类中,然后实现了dataSource方法,如下所示

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 14
    }

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
        cell.backgroundColor = UIColor.orangeColor()
        return cell
    }

相同的代码在app bundle中按预期工作。我不明白它为何在键盘扩展中有不同的行为。

我在这里缺少什么,我需要做些什么才能让它在键盘扩展中工作?

2 个答案:

答案 0 :(得分:1)

当您为collectionView提供约束时,将显示集合视图。

代码:

    collectionView.translatesAutoresizingMaskIntoConstraints = false
    self.view.addSubview(collectionView)

    collectionView.topAnchor.constraintEqualToAnchor(topLayoutGuide.bottomAnchor,constant: 0.0).active = true
    collectionView.bottomAnchor.constraintEqualToAnchor(bottomLayoutGuide.topAnchor, constant: 0.0).active = true
    collectionView.leadingAnchor.constraintEqualToAnchor(view.leadingAnchor).active = true
    collectionView.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor).active = true

答案 1 :(得分:0)

尝试一下兄弟:

override func viewDidLoad() {
super.viewDidLoad()

let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 10, height: 10)

collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
collectionView.backgroundColor = UIColor.whiteColor()
self.view.addSubview(collectionView)


// 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])

}

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.
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 14
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
cell.backgroundColor = UIColor.orangeColor()
return cell
}
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)`}`