【问题标题】:UICollectionView is not working in custom keyboard extensionUICollectionView 在自定义键盘扩展中不起作用
【发布时间】:2016-09-18 13:39:22
【问题描述】:

我正在尝试在自定义键盘扩展中实现 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)

我在我的类中添加了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
    }

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

我在这里有什么遗漏吗,我需要做什么才能让它在键盘扩展中工作?

【问题讨论】:

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


    【解决方案1】:

    当您为 collectionView 提供约束时,collection view 将可见。

    代码:

        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
    

    【讨论】:

      【解决方案2】:

      试试这个兄弟:

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

      【讨论】:

      • 我试过运行这个,我只能看到Next Keyboard 按钮。 UICollectionView 未显示
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-19
      相关资源
      最近更新 更多