Instagramのコメント投稿画面のようにキーボートのすぐ上にテキストボックスを設置したい場合、どうすればいいのだろうか?
(ちなみに、facebookでは下のような感じになる。)
Text, Web, and Editing Programming Guide for iOSを読むと、デフォルトでは216pxって書いてあるんだけど、日本語の推測変換が表示された場合、あきらかにそれ以上のようなwww
ってことで、通知してくれるdelegate機能みないなのってないのかなーっと思っていたらありました。
UIKeyboardWillShowNotification
UIKeyboardDidShowNotification
UIKeyboardWillHideNotification
UIKeyboardDidHideNotification
マニュアルを読むとUIWindowの中にあったので、UIWindowが通知してくれるのかな?
まーあまり細かいことを考えずに、下のようなソースで実装することができましたー。
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShown:) name:UIKeyboardWillShowNotification object:nil]; } -(void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; } -(void)keyboardWillShown:(NSNotification*)note{ NSDictionary* info = (NSDictionary*)[note userInfo]; CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; //Comment部分の位置の調節(kbSize.heightはキーボードの高さ) baseCommentView.frame = CGRectMake(0, (self.view.frame.size.height - kbSize.height - baseCommentView.frame.size.height), baseCommentView.frame.size.width, baseCommentView.frame.size.height); }これで、動的に位置が変わるテキストボックスの設置にも困ることはありませんね。
0 コメント:
コメントを投稿