鍵盤上附加個textField
沒有使用inputAccessoryView。
我是自己創建一個custom的UIView,裏面放了UITextField等控件。
實現的是類似iPhone短信那個界面,點擊下部UITextField編輯區域,彈出keyboard的東東。
思路是,你要監聽keyboard的Notification,通過獲取keyboard的frame,來調整裝有UITextField的那個UIView的frame,然後用animation
註冊通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
具體調整位置:
- (void)keyboardWillShow:(NSNotification *)notification {
/*
Reduce the size of the text view so that it's not obscured by the keyboard.
Animate the resize so that it's in sync with the appearance of the keyboard.
*/
NSDictionary *userInfo = [notification userInfo];
// Get the origin of the keyboard when it's displayed.
NSValue *boundsValue = [userInfo objectForKey:UIKeyboardBoundsUserInfoKey];
CGRect keyboardRect = [boundsValue CGRectValue];
// This UIKeyboardBoundsUserInfoKey returns CGRect won't be auto adapt to the interface orientation changes
// We should exchange the width and height manually
if (keyboardRect.size.width < keyboardRect.size.height)
{
float tempHeight = keyboardRect.size.height;
keyboardRect.size.height = keyboardRect.size.width;
keyboardRect.size.width = tempHeight;
}
// Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system.
//The bottom of the text view's frame should align with the top of the keyboard's final position.
CGFloat keyboardTop = self.view.frame.size.height - keyboardRect.size.height;
CGRect bottomViewFrame = bottomView.frame;
bottomViewFrame.origin.y = keyboardTop - bottomViewFrame.size.height;
// Get the duration of the animation.
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
// Animate the resize of the text view's frame in sync with the keyboard's appearance.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
bottomView.frame = bottomViewFrame;
[UIView commitAnimations];
keyBoardIsUp = YES;
CGRect tableViewFrame = listTableView.frame;
tableViewFrame.size.height = bottomViewFrame.origin.y - self.view.frame.origin.y;
listTableView.frame = tableViewFrame;
if (listTableView.contentSize.height > bottomViewFrame.origin.y)
{
listTableView.contentOffset = CGPointMake(0.0f, listTableView.contentSize.height - bottomViewFrame.origin.y);
}
}
- (void)keyboardWillHide:(NSNotification *)notification {
NSDictionary* userInfo = [notification userInfo];
/*
Restore the size of the text view (fill self's view).
Animate the resize so that it's in sync with the disappearance of the keyboard.
*/
CGRect bottomViewFrame = bottomView.frame;
bottomViewFrame.origin.y = self.view.frame.size.height - bottomViewFrame.size.height;
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
bottomView.frame = bottomViewFrame;
[UIView commitAnimations];
listTableView.frame = CGRectMake(self.listTableView.frame.origin.x, self.listTableView.frame.origin.y,listTableView.frame.size.width, self.view.frame.size.height - bottomView.frame.size.height);
if (listTableView.contentSize.height > listTableView.frame.size.height)
{
self.listTableView.contentOffset = CGPointMake(0.0f, listTableView.contentSize.height - listTableView.frame.size.height);
}
self.keyBoardIsUp = NO;
}
最後記得註銷通知:
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
我是自己創建一個custom的UIView,裏面放了UITextField等控件。
實現的是類似iPhone短信那個界面,點擊下部UITextField編輯區域,彈出keyboard的東東。
思路是,你要監聽keyboard的Notification,通過獲取keyboard的frame,來調整裝有UITextField的那個UIView的frame,然後用animation
註冊通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
具體調整位置:
- (void)keyboardWillShow:(NSNotification *)notification {
/*
Reduce the size of the text view so that it's not obscured by the keyboard.
Animate the resize so that it's in sync with the appearance of the keyboard.
*/
NSDictionary *userInfo = [notification userInfo];
// Get the origin of the keyboard when it's displayed.
NSValue *boundsValue = [userInfo objectForKey:UIKeyboardBoundsUserInfoKey];
CGRect keyboardRect = [boundsValue CGRectValue];
// This UIKeyboardBoundsUserInfoKey returns CGRect won't be auto adapt to the interface orientation changes
// We should exchange the width and height manually
if (keyboardRect.size.width < keyboardRect.size.height)
{
float tempHeight = keyboardRect.size.height;
keyboardRect.size.height = keyboardRect.size.width;
keyboardRect.size.width = tempHeight;
}
// Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system.
//The bottom of the text view's frame should align with the top of the keyboard's final position.
CGFloat keyboardTop = self.view.frame.size.height - keyboardRect.size.height;
CGRect bottomViewFrame = bottomView.frame;
bottomViewFrame.origin.y = keyboardTop - bottomViewFrame.size.height;
// Get the duration of the animation.
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
// Animate the resize of the text view's frame in sync with the keyboard's appearance.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
bottomView.frame = bottomViewFrame;
[UIView commitAnimations];
keyBoardIsUp = YES;
CGRect tableViewFrame = listTableView.frame;
tableViewFrame.size.height = bottomViewFrame.origin.y - self.view.frame.origin.y;
listTableView.frame = tableViewFrame;
if (listTableView.contentSize.height > bottomViewFrame.origin.y)
{
listTableView.contentOffset = CGPointMake(0.0f, listTableView.contentSize.height - bottomViewFrame.origin.y);
}
}
- (void)keyboardWillHide:(NSNotification *)notification {
NSDictionary* userInfo = [notification userInfo];
/*
Restore the size of the text view (fill self's view).
Animate the resize so that it's in sync with the disappearance of the keyboard.
*/
CGRect bottomViewFrame = bottomView.frame;
bottomViewFrame.origin.y = self.view.frame.size.height - bottomViewFrame.size.height;
NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];
bottomView.frame = bottomViewFrame;
[UIView commitAnimations];
listTableView.frame = CGRectMake(self.listTableView.frame.origin.x, self.listTableView.frame.origin.y,listTableView.frame.size.width, self.view.frame.size.height - bottomView.frame.size.height);
if (listTableView.contentSize.height > listTableView.frame.size.height)
{
self.listTableView.contentOffset = CGPointMake(0.0f, listTableView.contentSize.height - listTableView.frame.size.height);
}
self.keyBoardIsUp = NO;
}
最後記得註銷通知:
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.