2016-01-19 61 views
2

我想使用Swift做一個簡單的iOS自定義鍵盤擴展。默認的UIView不符合我的需要,所以我在this上讀到SO問題,我可以使用NSExtensionMainStoryboard屬性info.plist中的故事板。iOS自定義鍵盤與故事板

可惜每次我打開我的鍵盤時,我收到以下錯誤

2016-01-18 18:02:56.350 Kappa Keyboard[44790:2959180] Failed to inherit CoreMedia permissions from 44787: (null) 
2016-01-18 18:02:56.368 Kappa Keyboard[44790:2959138] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil' 
*** First throw call stack: 
(
    0 CoreFoundation      0x002f1746 __exceptionPreprocess + 182 
    1 libobjc.A.dylib      0x01d32a97 objc_exception_throw + 44 
    2 CoreFoundation      0x001a6ce1 -[__NSArrayM insertObject:atIndex:] + 881 
    3 CoreFoundation      0x001a6941 -[__NSArrayM addObject:] + 65 
    4 UIKit        0x00c7d0bd -[UIViewController _addChildViewController:performHierarchyCheck:notifyWillMove:] + 417 
    5 UIKit        0x00c968aa -[UIViewController(UIContainerViewControllerProtectedMethods) addChildViewController:] + 78 
    6 UIKit        0x01206db8 -[_UIViewServiceViewControllerOperator __createViewController:withContextToken:fbsDisplays:appearanceSerializedRepresentations:legacyAppearance:traitCollection:initialInterfaceOrientation:hostAccessibilityServerPort:canShowTextServices:replyHandler:] + 2702 

的堆棧跟蹤仍在繼續,但是這是它的要點。

這是我所做的一切(我在一個乾淨的項目中試過)。

  1. 創建容器應用
  2. 創建鍵盤目標
  3. 添加Main.storyboard
  4. 添加NSExtension.NSExtensionMainStoryboard: Main到Info.plist中
  5. 設置Main.storyboard的自定義類的默認KeyboardViewController

任何想法我失蹤?

回答

1

故事板文件添加到您的鍵盤目標。
在您的鍵盤目標的常規選項卡中,將屏幕截圖中的故事板文件製作爲Main Interface
第二個重要部分是確保將故事板中的參考視圖的類更改爲UIInputView而不是UIView

enter image description here

1

我也構建了一個自定義鍵盤應用程序。 步驟如下:

目標C

  1. 創建鍵盤目標
  2. 創建在目標束中的XIB文件(MyKeyboard.xib)
  3. 創建可可類觸摸選擇的UIView( MyKeyboard.h MyKeyboard.m)
  4. Inside KeyboardViewController.m,import MyKeyboard.h
  5. Inside KeyboardViewController.m,add

@property (nonatomic, strong) MyKeyboard *myKeyboard; 

viewDidLoad中:

self.myKeyboard = [[MyKeyboard alloc]init]; 
self.myKeyboard = [[[NSBundle mainBundle] loadNibName: @"MyKeyboard" 
            owner:self option:nil] objectAtIndex:0]; 
self.inputView = (UIInputView *)self.myKeyboard; 

斯威夫特

  1. 創建鍵盤目標
  2. 創建在目標束(MyKeyboard.xib)
  3. 創建可可類觸摸選擇UIInputView(MyKeyboard.swift)
  4. 內部KeyboardViewController一個UIView接口(XIB文件)。迅速加

//這裏

override func viewDidLoad() { 
    super.viewDidLoad() 
    // setup your keyboard 
    self.myKeyboard = NSBundle.mainBundle().loadNibNamed("MyKeyboard", owner: self, options: nil)[0] as! MyKeyboard 
    self.inputView = self.myKeyboard; 

    // 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.myKeyboard.addSubview(self.nextKeyboardButton) // add the button to self.myKeyboard!! 

    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]) 
} 

這裏是它會是什麼樣子 enter image description here

+0

我很欣賞你徹底,但我在尋找一個基於故事板,鍵盤,而不是一個文件的.xib。 – Harangue