2016-10-18 7 views
2

iOSのカスタムキーボードを作成しようとしています。このキーボードには、画像付きのコレクションビューが含まれています。現在、私はストーリーボードなしで試しています(ストーリーボードにはいくつかの問題があり、記述するのは簡単ではありません)。だから、僕は(別のボタンを追加し、その後、「nextKeyboardButton」(XCodeの上で新しいターゲットを追加する際にデフォルトでてくる)をUICollectionViewCell、最後にUICollectionView上のアイコンの種類を切り替えて追加していiOS、コレクションビューのカスタムキーボード、制約事項

私のコードは:。

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では認識されません。

答えて

0

collectionViewのtranslatesAutoresizingMaskIntoConstraintsをfalseに設定していますか?私はあなたがnextKeyboardButtonのためにやったことに気づいた。

Apple(?)のドキュメントを少し前に見ると、NSAutoresizingMaskLayoutConstraintのエラーが表示されたら、マスクがオフになっているかどうかを確認してください。

+0

私もそれを試しました。残念ながら、何も変わりません。 – manosim

0

ビューのサブビューとして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))

ので、コレクションビューの高さが指定されていることを確認します。さもなければ、それはゼロ高さに倒れるでしょう。

関連する問題