我正在尝试使用Swift for iOS 8构建自定义键盘,我以编程方式创建了每个按钮。当从内置键盘切换到我的自定义键盘时,它第一次非常慢,因为它花了2秒钟才出现。我不完全确定我是否正确地做到了。下面是我的代码,我只显示了2个按钮,但还有更多:
class KeyboardViewController: UIInputViewController {
@IBOutlet var nextKeyboardButton: UIButton!
@IBOutlet var qButton: UIButton!
var buttonFontSize:CGFloat = 22.0
var gapBtwButton:CGFloat = +7.0
// a lot more button below
override func updateViewConstraints() {
super.updateViewConstraints()
// Add custom view sizing constraints here
}
override func viewDidLoad() {
super.viewDidLoad()
setupEnKeyboard()
}
func setupEnKeyboard(){
println("setupEnKeyboard")
addEnKeyboardButtons()
}
func addEnKeyboardButtons() {
addQButton()
addNextKeyboardButton()
// a lot more button to be added
}
func setupButton(label: String, functionName: Selector, imageOnButton: String) -> UIButton {
// initialize the button
let keyButton:UIButton = UIButton.buttonWithType(.System) as UIButton
var testVar: String? = imageOnButton
if imageOnButton.isEmpty {
keyButton.setTitle(label, forState: .Normal)
} else {
let image = UIImage(named: imageOnButton) as UIImage
keyButton.setImage(image, forState: .Normal)
}
keyButton.sizeToFit()
keyButton.setTranslatesAutoresizingMaskIntoConstraints(false)
// adding a callback
keyButton.addTarget(self, action: functionName, forControlEvents: .TouchUpInside)
// make the font bigger
keyButton.titleLabel?.font = UIFont.systemFontOfSize(self.buttonFontSize)
// add rounded corners
keyButton.backgroundColor = UIColor(white: 0.9, alpha: 1)
keyButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
keyButton.layer.cornerRadius = 5
return keyButton
}
func addQButton() {
qButton = setupButton("Q", functionName:"didTapQButton", imageOnButton:"")
view.addSubview(qButton)
var leftSideConstraint = NSLayoutConstraint(item: qButton, attribute: .Left, relatedBy: .Equal, toItem: view, attribute: .Left, multiplier: 1.0, constant: +6.0)
var topConstraint = NSLayoutConstraint(item: qButton, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1.0, constant: +10.0)
view.addConstraints([leftSideConstraint, topConstraint])
}
func didTapQButton(){
var proxy = textDocumentProxy as UITextDocumentProxy
proxy.insertText("q")
}
func addNextKeyboardButton() {
nextKeyboardButton = setupButton("N", functionName:"advanceToNextInputMode", imageOnButton:"globe")
view.addSubview(nextKeyboardButton)
var leftSideConstraint = NSLayoutConstraint(item: nextKeyboardButton, attribute: .Left, relatedBy: .Equal, toItem: showNumbersButton, attribute: .Right, multiplier: 1.0, constant: self.gapBtwButton)
var bottomConstraint = NSLayoutConstraint(item: nextKeyboardButton, attribute: .Bottom, relatedBy: .Equal, toItem: view, attribute: .Bottom, multiplier: 1.0, constant: -3.0)
view.addConstraints([leftSideConstraint, bottomConstraint])
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated
}
override func textWillChange(textInput: UITextInput) {
// The app is about to change the document's contents. Perform any preparation here.
}
override func textDidChange(textInput: UITextInput) {
// The app has just changed the document's contents, the document context has been updated.
var textColor: UIColor
var proxy = self.textDocumentProxy as UITextDocumentProxy
if proxy.keyboardAppearance == UIKeyboardAppearance.Dark {
textColor = UIColor.whiteColor()
} else {
textColor = UIColor.blackColor()
}
//self.nextKeyboardButton.setTitleColor(textColor, forState: .Normal)
}
}
请欣赏任何评论:)
谢谢, Mark Thien