iOS,带有“收藏夹"视图的自定义键盘,约束问题 [英] iOS, Custom Keyboard with Collection View, Constraints Issue

查看:93
本文介绍了iOS,带有“收藏夹"视图的自定义键盘,约束问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为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上,但是我不明白为什么这是错误的,并且使它失败.

Ps.在模拟器中,它可以正常工作,但在iPhone 6S中却不能.

解决方案

您是否将collectionView的translationsAutoresizingMaskIntoConstraints设置为false?我注意到您为nextKeyboardButton做过.

我记得前段时间看到过Apple(?)文档,该文档说如果您看到NSAutoresizingMaskLayoutConstraint列出的错误,请检查是否已关闭蒙版.

I am trying to create a custom keyboard for iOS which includes a collection view with images. Currently I am trying without a storyboard(had several issues with the storyboard and it is not easy to describe). So I am just adding the "nextKeyboardButton" (comes by default when adding the new target on XCode), then added another button (switches the icon types on the UICollectionViewCell and finally the UICollectionView.

My 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: "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
    }
}

The error I am getting is:

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)>"
)

So I can see that the error is probably at UICollectionView:0x11000aa00.bottom == UIInputView:0x10fe00990.bottom but I cannot understand why this is wrong and it is making it fail.

Ps. In the simulator it is working find but it does not in an iPhone 6S.

解决方案

Are you setting the collectionView's translatesAutoresizingMaskIntoConstraints to false? I noticed you did for the nextKeyboardButton.

I remember seeing Apple(?) documentation a while ago that said if you see errors listed for NSAutoresizingMaskLayoutConstraint, check that the mask is turned off.

这篇关于iOS,带有“收藏夹"视图的自定义键盘,约束问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆