带故事板的iOS自定义键盘

时间:2016-01-19 00:14:12

标签: ios iphone xcode swift keyboard

我尝试使用Swift制作简单的iOS自定义键盘扩展程序。默认的UIView并不能满足我的需求,因此我在this上阅读了问题,我可以使用Cbc属性的故事板。

不幸的是每次我打开键盘时,都会出现以下错误

C.print(B)

堆栈跟踪继续,但这是它的要点。

这就是我所做的一切(我在一个干净的项目中试过这个)。

  1. 创建容器应用
  2. 创建键盘目标
  3. 添加Main.storyboard
  4. NSExtensionMainStoryboard添加到info.plist
  5. info.plist的自定义类设置为默认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
  6. 知道我错过了什么吗?

2 个答案:

答案 0 :(得分:2)

将故事板文件添加到键盘目标 在键盘目标的常规标签页中,将Main Interface设置为故事板文件,如屏幕截图所示 第二个重要的部分是确保将您的KeyboardViewController参考视图的课程更改为UIInputView而不是故事板中的UIView

enter image description here

答案 1 :(得分:1)

我也建立了一个自定义键盘应用程序。 以下是步骤:

<强>目标C

  1. 创建键盘目标
  2. 在目标包中创建一个xib文件(MyKeyboard.xib)
  3. 创建cocoa touch类选择UIView(MyKeyboard.h MyKeyboard.m)
  4. 在KeyboardViewController.m中,导入MyKeyboard.h
  5. 在KeyboardViewController.m中,添加
  6. @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. 在目标包中创建一个UIView接口(xib文件)(MyKeyboard.xib)
    3. 创建cocoa touch类选择UIInputView(MyKeyboard.swift)
    4. 在KeyboardViewController.swift中添加
    5. // here

      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