キーボードの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