點(diǎn)擊UITextView鍵盤彈出時(shí),上移vc.view,出現(xiàn)的問題.
之前為防止鍵盤擋住UITextView,我是這么做的:
先注冊了鍵盤的相關(guān)通知:
self.myTextView = [[UITextView alloc] initWithFrame:CGRectMake(100, 450, 200, 44)];//這里特意讓UITextView處于屏幕右下角,這樣如果self.view不上移,肯定會(huì)被鍵盤擋住的
self.myTextView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:self.myTextView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
然后在鍵盤通知響應(yīng)方法中處理
//鍵盤將要出現(xiàn)
- (void)handleKeyboardWillShow:(NSNotification *)paramNotification
{
NSLog(@"鍵盤即將出現(xiàn)");
NSValue *value = [[paramNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];//使用UIKeyboardFrameBeginUserInfoKey,會(huì)出現(xiàn)切換輸入法時(shí)獲取的搜狗鍵盤不對.
CGRect keyboardRect = [value CGRectValue];
CGFloat keyboardH = keyboardRect.size.height;
NSLog(@"鍵盤高度:%f", keyboardH);
self.view.transform = CGAffineTransformMakeTranslation(0, -keyboardH);
}
//鍵盤將要隱藏
- (void)handleKeyboardWillHide:(NSNotification *)paramNotification
{
NSLog(@"鍵盤即將隱藏");
self.view.transform = CGAffineTransformIdentity;
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
[self.view endEditing:YES];
}
這種辦法,在VC沒有navigationBar時(shí)沒有什么bug.
但是當(dāng)VC帶有navigationBar時(shí),就出問題了(就我現(xiàn)在這個(gè)demo來說).
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// self.window.backgroundColor = [UIColor whiteColor];
ViewController *vc = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
nav.navigationBar.translucent = NO;
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
當(dāng)點(diǎn)擊UITextView鍵盤彈出后,按下home鍵,然后再進(jìn)入應(yīng)用,就會(huì)看到UITextView不在鍵盤上方了,且點(diǎn)擊屏幕讓UITextView失去焦點(diǎn)后會(huì)看到頂端有一個(gè)黑色的view,目測應(yīng)該是window.
那么如何解決呢?我這里只是寫死坐標(biāo)了.
//鍵盤將要顯現(xiàn)
- (void)handleKeyboardWillShow:(NSNotification *)paramNotification
{
NSLog(@"鍵盤即將出現(xiàn)");
NSValue *value = [[paramNotification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];//使用UIKeyboardFrameBeginUserInfoKey,會(huì)出現(xiàn)切換輸入法時(shí)獲取的搜狗鍵盤不對.
CGRect keyboardRect = [value CGRectValue];
CGFloat keyboardH = keyboardRect.size.height;
NSLog(@"鍵盤高度:%f", keyboardH);
CGRect r = self.view.frame;
r.origin.y = 64 - keyboardH;
self.view.frame = r;
}
//鍵盤將要隱藏
- (void)handleKeyboardWillHide:(NSNotification *)paramNotification
{
NSLog(@"鍵盤即將隱藏");
CGRect r = self.view.frame;
r.origin.y = 64;
self.view.frame = r;
}
以為大功告成了,然而bug總是不期而遇,上面的解決辦法在iOS9上沒問題了,但是在iOS8上又有問題,當(dāng)點(diǎn)擊UITextView鍵盤彈出后,按下home鍵,然后再進(jìn)入應(yīng)用,會(huì)發(fā)現(xiàn)鍵盤還在但鍵盤上方的UITextView又不見了,但點(diǎn)擊屏幕讓UITextView失去焦點(diǎn)后,才會(huì)再看到UITextView.
那么如何解決呢?暫時(shí)沒想到有啥好辦法.目前的辦法是讓它在進(jìn)入后臺時(shí)失去焦點(diǎn).
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
- (void)applicationDidEnterBackground:(NSNotification *)paramNotification
{
NSLog(@"應(yīng)用已經(jīng)進(jìn)入后臺,%@", paramNotification);
[self.myTextView resignFirstResponder];
}
小伙伴們有更好的辦法歡迎留言.