LoginSignup
57
57

More than 5 years have passed since last update.

iOSでキーボードがニョキッと出てきてUIScrollView上の入力フォームが隠れる問題を0.5秒でフィクスする

Last updated at Posted at 2014-08-28

実際は多分50秒くらいかかります

つかみが大切なので悪しからず。

UIScrollView上のUITextFieldに入力しようとタップするとキーボードがせり上がってきますよね。そのキーボードでこれから入力しようとしているUITextFieldが隠れるのを解決する方法です。

やり方

Cocoapodを使って1つだけ入れてください。

Podfile
pod "EKKeyboardAvoiding"

中にUIScrollViewのカテゴリが含まれてます。

UIScrollView+EKKeyboardAvoiding.h
#import <UIKit/UIKit.h>
@interface UIScrollView (EKKeyboardAvoiding)
- (BOOL)keyboardAvoidingEnabled;
- (void)setKeyboardAvoidingEnabled:(BOOL)enabled;
@end

見ての通りUIScrollViewのインスタンスでsetKeyboardAvoidingEnabled:YESを呼んでやれば、キーボードが出てきたときにその分スクロールしてくれます。スクロールが一番下まで来ていても、なんというかオーバースクロール?してくれます。

ストーリーボードだけで解決する

上記のヘッダファイルは、正確にはこう言ってしまっていいのかは分かりませんが、俺的には

UIScrollView+EKKeyboardAvoiding.h
#import <UIKit/UIKit.h>
@interface UIScrollView (EKKeyboardAvoiding)
@property (readwrite) BOOL keyboardAvoidingEnabled;
@end

と等価だと思ってます。

なのでストーリーボードで対象のUIScrollViewを選択しUser Defined Runtime AttributesにkeyboardAvoidingEnabledというキーを追加しBOOLでチェックボックスをオン(YES)にします。

こうすればコードを一切書かずにフィクス出来ます。

57
57
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
57
57