2016-05-21 3 views
1

Я пытаюсь реализовать UICollectionView в пользовательском расширении клавиатуры, но он не работает. Это то, что я делаю внутри метода viewDidLoad.UICollectionView не работает в пользовательском расширении клавиатуры

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) 

Я добавил UICollectionViewDelegateFlowLayout и UICollectionViewDataSource протоколы к моему классу, а затем реализованы методы 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 
    } 

тот же код работает, как ожидалось в приложении пачке. Я не понимаю, почему у него другое поведение в расширении клавиатуры.

Есть ли что-нибудь, что мне не хватает здесь, Что мне нужно сделать, чтобы заставить его работать в расширении клавиатуры?

ответ

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 
+0

Он работает для меня, спасибо большое prad :) – Rashid

+0

Это сработало и для меня, спасибо! – theMouse

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)`}` 
+0

Я попытался запустить это, я могу видеть только 'кнопку Next Keyboard'. UICollectionView не отображается. – Rashid

Смежные вопросы