2015-11-03 56 views
2

我創建了自定義鍵盤,它工作正常。但我不知道如何將我的自定義鍵盤設置爲特定的UITextField。 這是我的KeyboardViewController如何將Custom Keybo作爲默認鍵盤設置爲UITextView?

import UIKit 

class KeyboardViewController: UIInputViewController { 

    @IBOutlet var nextKeyboardButton: UIButton! 
    @IBOutlet weak var percentView: UIView! 
    @IBOutlet weak var hideKeyboardButton: UIButton! 

    override func updateViewConstraints() { 
     super.updateViewConstraints() 

     // Add custom view sizing constraints here 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Perform custom UI setup here 
     self.nextKeyboardButton = UIButton(type: .System) 

     self.nextKeyboardButton.setTitle(NSLocalizedString("Next Keyboard", comment: "Title for 'Next Keyboard' button"), forState: .Normal) 
     self.nextKeyboardButton.sizeToFit() 
     self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = false 

     self.nextKeyboardButton.addTarget(self, action: "advanceToNextInputMode", forControlEvents: .TouchUpInside) 

     self.view.addSubview(self.nextKeyboardButton) 


     let nextKeyboardButtonLeftSideConstraint = NSLayoutConstraint(item: self.nextKeyboardButton, attribute: .Left, relatedBy: .Equal, toItem: self.view, attribute: .Left, multiplier: 1.0, constant: 0.0) 
     let nextKeyboardButtonBottomConstraint = NSLayoutConstraint(item: self.nextKeyboardButton, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1.0, constant: 0.0) 
     self.view.addConstraints([nextKeyboardButtonLeftSideConstraint, nextKeyboardButtonBottomConstraint]) 

     let nib = UINib(nibName: "KeyboardView", bundle: nil) 
     let objects = nib.instantiateWithOwner(self, options: nil) 
     view = objects[0] as! UIView; 

     for view in self.view.subviews { 
      if let btn = view as? UIButton { 
       btn.layer.cornerRadius = 5.0 
       btn.translatesAutoresizingMaskIntoConstraints = false 
       //btn.addTarget(self, action: "keyPressed:", forControlEvents: .TouchUpInside) 
      } 
     } 
    } 

和其他委託方法。那我怎麼稱呼它呢? 其實問題是我無法從應用程序擴展中導入自定義鍵盤類。這是主要問題。我做了所有構建階段的東西。如果我自己更換鍵盤,我可以使用鍵盤。

+0

您是否想要將自定義字體添加到UITextField中,或者只是將該鍵盤用於特定文本字段? –

+0

不是。我已創建數字鍵盤作爲iPad應用程序的自定義鍵盤。所以我需要將此鍵盤設置爲默認鍵盤的某些特定UITextFiled。 – letitbefornow

+0

您可能會在這裏找到您的答案,[customkeyboard](http://stackoverflow.com/questions/33474771/a-swift-example-of-custom-views-for-data-input-custom-in-app-keyboard) – MBTDesigns

回答

2

根據Apple Documentation

當用戶選擇一個自定義鍵盤,它變成了鍵盤用戶打開的所有應用。

因此,它不像你的應用特定的東西,它更像是不同的語言鍵盤。只要你不能爲你的UITextField或UITextView指定一些特定的語言鍵盤,你也不能指定你的自定義鍵盤。

您確定您需要自定義鍵盤而不是自定義視圖來輸入應用程序中的數據嗎?關於自定義數據輸入視圖,您可以閱讀here

+0

你可以用代碼來證明它嗎? – letitbefornow

+0

當然,我編輯了這個問題。 – letitbefornow

+0

其實在obj-c我也可以做到這一點。但問題是我無法從應用程序擴展中導入自定義鍵盤類。這是主要問題。 – letitbefornow