监听键盘的出现和隐藏顶部View值得改变
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了监听键盘的出现和隐藏顶部View值得改变相关的知识,希望对你有一定的参考价值。
- (UITextView *)publishTextView
{
if (!_publishTextView)
{
UITextView *publishTextView = [[UITextView alloc] init];
publishTextView.font = ContentTitleFont;
publishTextView.frame = CGRectMake(0, 0, Screen_Width, Screen_Height);
// 键盘成为第一响应者
[publishTextView becomeFirstResponder];
self.publishTextView = publishTextView;
[self.view addSubview:publishTextView];
}
return _publishTextView;
}
- (YSWeiBoPublishToolbar *)publishToolbar
{
if (!_publishToolbar)
{
YSWeiBoPublishToolbar *publishToolbar = [[YSWeiBoPublishToolbar alloc] init];
CGFloat publishToolbarHeight = 44.f;
publishToolbar.frame = CGRectMake(0, Screen_Height - publishToolbarHeight, Screen_Width, publishToolbarHeight);
self.publishToolbar = publishToolbar;
[self.view addSubview:publishToolbar];
}
return _publishToolbar;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.publishTextView.delegate = self;
self.publishToolbar;
[self setupNavigationItem];
[YSWeiBoNotificationCenter addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
#pragma mark - 监听方法
/** */
- (void)keyboardWillChangeFrame:(NSNotification *)notification
{
NSDictionary *userInfo = notification.userInfo;
// 动画的持续时间
double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 键盘的frame
CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
[UIView animateWithDuration:duration animations:^{
if (keyboardF.origin.y > self.view.height) { // 键盘的Y值已经远远超过了控制器view的高度
self.publishToolbar.y = self.view.height - self.publishToolbar.height;
} else {
self.publishToolbar.y = keyboardF.origin.y - self.publishToolbar.height;
}
}];
}
以上是关于监听键盘的出现和隐藏顶部View值得改变的主要内容,如果未能解决你的问题,请参考以下文章