キーボードに閉じるボタンをつけるメモ
文字列入力するときのキーボードに、閉じるボタンとか付いてるのはよく見るけど、
設定?と思ってました。が、どうやら、キーボード(というか、UITextField)のAccessoryViewに
インスタンスしたViewをセットしてあげれば良いらしい。
一昨日のUITableViewに追加してみた。
で、TextFieldの設定のところで、追加する。
LoginViewController.m
// テーブルの列にデータセット - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITextField *passTextFld; :インスタンス&設定 // テキスト passTextFld = [[UITextField alloc] initWithFrame:CGRectMake(130.0f, 0.0f, 140.0f, 50.0f)]; passTextFld.delegate = self; :設定 passTextFld.inputAccessoryView = [self getAccessoryView]; if (indexPath.row == 0) { [nameLabel setText:@"ユーザーID"]; : } else { [nameLabel setText:@"パスワード"]; : } }
AccessoryView生成&取得
- (UIView *)getAccessoryView { UIView *accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 280.0f, 40.0f)]; accessoryView.backgroundColor = [UIColor blueColor]; UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(250.0f, 5.0f, 65.0f, 34.0f); [button setTitle:@"とじる" forState:UIControlStateNormal]; // ボタンを押した時のイベント [button addTarget:self action:@selector(closeKeyboard) forControlEvents:UIControlEventTouchUpInside]; // View にボタン追加 [accessoryView addSubview:button]; return accessoryView; }
ハイ。できました♪
…
同じテキストフィールドにセットしてるのに。orz
UITableViewのIndexPathが違うだけデス。先頭のユーザーID(UITableViewのRowIndex=0)はダメで、
パスワードはOK。cellForIndexPath…双方共設定してるつもりなのに…
先は長い。
【参考サイト】
☆キーボードを閉じるためのボタンを追加する
http://works.sabitori.com/2011/06/18/add-button-to-keyboard/
☆Customizing the iOS keyboard
http://blog.carbonfive.com/2012/03/12/customizing-the-ios-keyboard/