How to put UILabel in "inputAccessoryView" for keyboard?

I was able to get the button (with image) in the inputAccessoryView property for the keyboard to work (cameraButton). However, when trying to place a UILabel (cameraLabel) to the right of the camera, Xcode throws an error - "[UILabel view]: unrecognized selector sent to instance 0x7fd66ca31a30"

This is my first time trying inputAccessoryView and creating the UILabel programmatically. Here's my code and I've searched here and Google for hours and can't find the exact same scenario I'm trying to achieve. Any help is greatly appreciated !:

First part:

class SecondViewController: UIViewController, UITextViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

var cameraLabel: UILabel!

      

In viewDidLoad mode (these are the things I have collected from various online resources):

 let keyboardButtonView = UIToolbar()
    keyboardButtonView.sizeToFit()


    let imageCamera = UIImage(named: "camera")?.imageWithRenderingMode(.AlwaysOriginal)
    let cameraButton = UIBarButtonItem(image: imageCamera, style: .Plain, target: self, action: "keyboardCamera")

    //not sure how x & y for CGRect work, especially within the inputAccessoryView. Just guessed on the width and height for now, until I can get it to work.

    cameraLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 50, height: 40))
    cameraLabel.numberOfLines = 1
    cameraLabel.adjustsFontSizeToFitWidth = true
    cameraLabel.text = "Optionally Add Photo"
    cameraLabel.font = UIFont.boldSystemFontOfSize(10)
    var toolbarButtons = NSMutableArray()
    toolbarButtons.addObject(cameraButton)
    toolbarButtons.addObject(cameraLabel)


   keyboardButtonView.items = toolbarButtons

    //Alternatively, another person did this....
     keyboardButtonView.setItems(toolbarButtons, animated: false)

    textView.inputAccessoryView = keyboardButtonView

      

+3


source to share


1 answer


The link added above provided the answer. I had to put the UILabel as a custom view of the UIBarButtonItem. This was additional code for this -



let cameraLabBut = UIBarButtonItem(customView: cameraLabel)

      

+1


source







All Articles