我尝试使用Swift制作简单的iOS自定义键盘扩展程序。默认的UIView并不能满足我的需求,因此我在this上阅读了问题,我可以使用C
中bc
属性的故事板。
不幸的是每次我打开键盘时,都会出现以下错误
C.print(B)
堆栈跟踪继续,但这是它的要点。
这就是我所做的一切(我在一个干净的项目中试过这个)。
NSExtensionMainStoryboard
添加到info.plist 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
知道我错过了什么吗?
答案 0 :(得分:2)
将故事板文件添加到键盘目标
在键盘目标的常规标签页中,将Main Interface
设置为故事板文件,如屏幕截图所示
第二个重要的部分是确保将您的KeyboardViewController
参考视图的课程更改为UIInputView
而不是故事板中的UIView
。
答案 1 :(得分:1)
我也建立了一个自定义键盘应用程序。 以下是步骤:
<强>目标C 强>
@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;
<强>夫特强>
// 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])
}