カスタムキーボード | iPhoneの開発を始めました。

カスタムキーボード

iphoneのSDKで提供しているキーボードの中には色々ある。

その中にナンバーパッドがあるんだが、これが整数しか入力が出来ないし、完了ボタンもない。

とりあえず使いにくいのだ。



という事で、今度はナンバーパッドの左下の使わない余白を使おうというコードを探してみた。


うん。自分で作っては無い。探した。笑


んで見つかったのがこれだ。


ま、まずはxibでボタンを貼付けたuiviewが必要だね。 (プログラミングで作っても良いけど)


そしてこれ。

このメソッドをキーボードが出てきてから、(アニメーション中にボタンをのせるとボタンが消えてしまう)呼び出す。

パーフォーマセレクターで0.5秒ぐらいが適度かな?


- (void) custumKeyboard
{
//The UIWindow that contains the keyboard view
UIWindow* tempWindow;

//Because we cant get access to the UIKeyboard throught the SDK we will just use UIView.
//UIKeyboard is a subclass of UIView anyways
UIView* keyboard;

//Check each window in our application
for(int c = 0; c < [[[UIApplication sharedApplication] windows] count]; c ++)
{
//Get a reference of the current window
tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:c];

//Get a reference of the current view
for(int i = 0; i < [tempWindow.subviews count]; i++)
{
keyboard = [tempWindow.subviews objectAtIndex:i];

バージョンによってキーボードのprefixが違うみたい。(prefixってなんだろう。。笑)
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2)
{
if([[keyboard description] hasPrefix:@" {
//Keyboard is now a UIView reference to the UIKeyboard we want. From here we can add a subview
//to th keyboard like a new button
buttomView_は俺が作ったボタンをのせたビュー。これでオッケー
[keyboard addSubview: buttonView_];
return;
}
}
else
{
if([[keyboard description] hasPrefix:@" {
//Keyboard is now a UIView reference to the UIKeyboard we want. From here we can add a subview
//to th keyboard like a new button
[keyboard addSubview: buttonView_];
return;
}
}

}
}
}


これで、SDKで提供しているキーボードの上にViewをのせる事が出来るのだ。

これを考えた人はすごいよね。

キーボードが強制的に一番手前にくるから、そのキーボードのビューを探してそのキーボードのビューにaddSubviewをすればキーボードの上に何かを表示出来る訳だ。

んで、それを使って俺はボタンを追加した。


こうみれは簡単だけど、これが思いつかなかったんだよね。
まだまだプログラマーの道はこれからって事だ。