【问题标题】:iOS, Custom Keyboard with Collection View, Constraints IssueiOS,带有集合视图的自定义键盘,约束问题
【发布时间】:2016-10-18 16:07:52
【问题描述】:

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

我的代码:

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: "Cell")
        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("ABC", comment: "Title for 'Next Keyboard' button"), 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("View Gifs", 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
    }
}

我得到的错误是:

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

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

附言。在模拟器中可以找到,但在 iPhone 6S 中却没有。

【问题讨论】:

    标签: ios swift layout uicollectionview constraints


    【解决方案1】:

    在将 collectionView 作为子视图添加到视图之前添加此行:

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

    和 addSubview 之后的这一行: self.view.addConstraint(NSLayoutConstraint(item: self.collectionView, attribute: .height, relatedBy: .equal, toItem: self.view, attribute: .height, multiplier: 1.0, constant: 0.0))

    以便指定集合视图的高度。否则它将塌陷到零高度。

    【讨论】:

      【解决方案2】:

      您是否将 collectionView 的 translatesAutoresizingMaskIntoConstraints 设置为 false?我注意到你为 nextKeyboardButton 做了。

      我记得前段时间看到 Apple(?) 文档说,如果您看到为 NSAutoresizingMaskLayoutConstraint 列出的错误,请检查掩码是否已关闭。

      【讨论】:

      • 我也试过了 - 不幸的是它并没有改变任何东西。
      猜你喜欢
      • 2018-12-04
      • 2017-02-22
      • 1970-01-01
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-08
      相关资源
      最近更新 更多