Skip to content

Customize Utility Input ViewController

howardfuntek edited this page Oct 27, 2019 · 2 revisions

First, inherent IMUtilityInputViewController with your own customized class. Then override following three methods down below.

import IMKit

class UtilityInputViewController: IMUtilityInputViewController {
 
    // UICollectionViewDelegate
    
    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        switch IMUtilityType.allCases[indexPath.row] {
        case .Location:
           (viewController as? IMMessagesViewController)?.presentLocationPicker()
        case .File:
           (viewController as? IMMessagesViewController)?.presentDocumentPicker()
        }
        
        // handle your extra utilities
        // viewController?.present(UIViewController(), animated: true, completion: nil)
    }
    
    // UICollectionViewDataSource
    
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // you can add extra data source for additional utilities
        return IMUtilityType.allCases.count
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "IMUtilityCollectionViewCell", for: indexPath) as! IMUtilityCollectionViewCell
        cell.type = IMUtilityType.allCases[indexPath.row]
        
        // for your own customized cell
        cell.imageView.image = UIImage(named: "icon_pin_w24_h24", in: IMKit.bundle, compatibleWith: nil)
        cell.label.text = "n.location".IMLocalized
        
        return cell
    }
}

Seconds, inherent IMMessagesViewController with your own customized class to override utilityInputViewController

import IMKit

class MessagesViewController: IMMessagesViewController {
    
    lazy var utilityInputVC: UtilityInputViewController = {
        let vc = UtilityInputViewController()
        vc.viewController = self
        return vc
    }()
    
    override var utilityInputViewController: IMUtilityInputViewController {
        get {
            return utilityInputVC
        }
        set {}
    }
}

Demo App

Clone this wiki locally