3

我能够在键盘的 inputAccessoryView 属性中获得一个按钮(带图像)来工作(cameraButton)。但是,当尝试将 UILabel (cameraLabel) 放在 cameraButton 的右侧时,Xcode 会抛出错误 - “[UILabel view]: unrecognized selector sent to instance 0x7fd66ca31a30”

这是我第一次尝试 inputAccessoryView 并以编程方式创建 UILabel。这是我的代码,我已经在这里和谷歌搜索了几个小时,但找不到我想要实现的相同场景。很感谢任何形式的帮助!:

第一部分:

class SecondViewController: UIViewController, UITextViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

var cameraLabel: UILabel!

在 viewDidLoad 方法下(这些是我从各种在线资源中拼凑的东西):

 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
4

1 回答 1

1

上面添加的“灵魂”链接提供了答案。我不得不将 UILabel 作为 UIBarButtonItem 的自定义视图。这是执行此操作的额外代码 -

let cameraLabBut = UIBarButtonItem(customView: cameraLabel)
于 2014-12-29T02:53:57.587 回答