如何将UILabel放入键盘的“inputAccessoryView”中?

时间:2014-12-28 04:17:34

标签: ios swift keyboard uilabel inputaccessoryview

我能够在键盘的inputAccessoryView属性中获得一个按钮(带图像)(cameraButton)。但是,当试图将UILabel(cameraLabel)放在cameraButton的右侧时,Xcode会抛出错误 - " [UILabel视图]:无法识别的选择器发送到实例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

1 个答案:

答案 0 :(得分:1)

" soulshined"的链接上面补充说明了答案。我不得不将UILabel作为UIBarButtonItem的自定义视图。这是执行此操作的额外代码 -

let cameraLabBut = UIBarButtonItem(customView: cameraLabel)