Renee Olson
Renee Olson

Reputation: 267

How to put UILabel in "inputAccessoryView" for keyboard?

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

This is my first time trying an inputAccessoryView and creating a UILabel programmatically. Here's my code and I've been searching here and google for hours and can't find same scenario I'm trying to achieve. Any help is very much appreciated!:

First part:

class SecondViewController: UIViewController, UITextViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

var cameraLabel: UILabel!

Under viewDidLoad method (these are things I've pieced together 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

Upvotes: 3

Views: 1728

Answers (1)

Renee Olson
Renee Olson

Reputation: 267

The link that "soulshined" added above provided the answer. I had to put the UILabel as a custom view of the UIBarButtonItem. This was the extra code to do this -

let cameraLabBut = UIBarButtonItem(customView: cameraLabel)

Upvotes: 1

Related Questions