菜鸟教程小白 发表于 2022-12-11 17:52:55

iOS,带有 Collection View 的自定义键盘,约束问题


                                            <p><p>我正在尝试为 iOS 创建一个自定义键盘,其中包括带有图像的 Collection View 。目前我正在尝试没有 Storyboard ( Storyboard 有几个问题,很难描述)。所以我只是添加“nextKeyboardButton”(在 XCode 上添加新目标时默认出现),然后添加另一个按钮(切换 <code>UICollectionViewCell</code> 上的图标类型,最后是 <code>UICollectionView</code>.</p>

<p>我的代码:</p>

<pre><code>class KeyboardViewController: UIInputViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

    @IBOutlet var nextKeyboardButton: UIButton!
    @IBOutlet var switchTypedButton: UIButton!

    var isEmoji: Bool! = true;
    var collectionView: UICollectionView!

    override func viewDidLoad() {
      super.viewDidLoad()

      // Collection View
      let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
      layout.scrollDirection = UICollectionViewScrollDirection.horizontal
      layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 30, right: 10)
      layout.itemSize = CGSize(width: 50, height: 50)

      collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
      collectionView.dataSource = self
      collectionView.delegate = self
      collectionView.register(IconViewCell.self, forCellWithReuseIdentifier: &#34;Cell&#34;)
      collectionView.backgroundColor = UIColor.white
      collectionView.showsHorizontalScrollIndicator = false

      self.view.addSubview(collectionView)


      // Perform custom UI setup here
      self.nextKeyboardButton = UIButton(type: .system)
      self.nextKeyboardButton.setTitle(NSLocalizedString(&#34;ABC&#34;, comment: &#34;Title for &#39;Next Keyboard&#39; button&#34;), for: [])
      self.nextKeyboardButton.sizeToFit()
      self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = false
      self.nextKeyboardButton.addTarget(self, action: #selector(handleInputModeList(from:with:)), for: .allTouchEvents)

      self.view.addSubview(self.nextKeyboardButton)

      // Perform custom UI setup here
      self.switchTypedButton = UIButton(type: .system)
      self.switchTypedButton.setTitle(&#34;View Gifs&#34;, for: [])
      self.switchTypedButton.sizeToFit()
      self.switchTypedButton.translatesAutoresizingMaskIntoConstraints = false
      self.switchTypedButton.addTarget(self, action: #selector(self.switchTypedFunction), for: .touchUpInside)

      self.view.addSubview(self.switchTypedButton)

      self.nextKeyboardButton.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 10).isActive = true
      self.nextKeyboardButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true

      self.switchTypedButton.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -10).isActive = true
      self.switchTypedButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true

      self.collectionView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
      self.collectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
      self.collectionView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
      self.collectionView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
    }
}
</code></pre>

<p>我得到的错误是:</p>

<pre><code>Probably at least one of the constraints in the following list is one you don&#39;t want.
Try this:
    (1) look at each constraint and try to figure out which you don&#39;t expect;
    (2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you&#39;re seeing NSAutoresizingMaskLayoutConstraints that you don&#39;t understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
&#34;&lt;NSAutoresizingMaskLayoutConstraint:0x174097c00 h=--&amp; v=--&amp; UICollectionView:0x11000aa00.height == 0   (active)&gt;&#34;,
&#34;&lt;NSLayoutConstraint:0x174097430 V:|-(0)-   (active, names: &#39;|&#39;:UIInputView:0x10fe00990 )&gt;&#34;,
&#34;&lt;NSLayoutConstraint:0x1740974d0 UICollectionView:0x11000aa00.bottom == UIInputView:0x10fe00990.bottom   (active)&gt;&#34;,&lt;&lt;&lt;&lt;&lt;&lt;&lt;------- ERROR
&#34;&lt;NSLayoutConstraint:0x174097930 &#39;UIView-Encapsulated-Layout-Height&#39; UIInputView:0x10fe00990.height == 667   (active)&gt;&#34;
)
</code></pre>

<p>所以我可以看到错误可能出现在 <code>UICollectionView:0x11000aa00.bottom == UIInputView:0x10fe00990.bottom</code> 但我不明白为什么这是错误的并且它导致它失败。</p >

<p>附言。在模拟器中可以找到,但在 iPhone 6S 中却没有。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>在将 collectionView 作为 subview 添加到 View 之前添加此行:</p>

<p><code>
collectionView.translatesAutoresizingMaskIntoConstraints = false
</code>
这样自动调整大小的约束就被删除了</p>

<p>和 addSubview 之后的这一行:
<code>
self.view.addConstraint(NSLayoutConstraint(项目:self.collectionView,属性:.height,relatedBy:.equal,toItem:self.view,属性:.height,乘数:1.0,常数:0.0))
</code></p>

<p>以便指定 Collection View 的高度。否则它将折叠到零高度。</p></p>
                                   
                                                <p style="font-size: 20px;">关于iOS,带有 Collection View 的自定义键盘,约束问题,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/40113114/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/40113114/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS,带有 Collection View 的自定义键盘,约束问题