【问题标题】:How to put UILabel in "inputAccessoryView" for keyboard?如何将 UILabel 放在键盘的“inputAccessoryView”中?
【发布时间】:2014-12-28 04:17:33
【问题描述】:

我能够在键盘的 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

【问题讨论】:

  • 您尝试将标签添加到功能不同的工具栏,因为工具栏通常由按钮组成。看看这个问题并将其翻译成 swift stackoverflow.com/questions/9529106/…
  • 谢谢。我不是最擅长解释 obj-c,但看起来我可能必须在 UIBarButtonItem 中放置一个 customview UILabel。第二个答案使用了一种叫做释放的东西。但我不确定。
  • 不用担心现在使用 ARC 会自动完成发布。我只是将您发送给他们的对话,因为它类似于您的最终目标
  • 谢谢!它为我指明了正确的方向!
  • @soulshined 成功了。再次感谢!

标签: ios swift keyboard uilabel inputaccessoryview


【解决方案1】:

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

let cameraLabBut = UIBarButtonItem(customView: cameraLabel)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    • 2014-11-27
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    相关资源
    最近更新 更多