1、在viewDidLoad方法中加入监测键盘的通知。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
}
2、实现通知的方法
/**
* 键盘将要显示
*
* @param notification 通知
*/
-(void)keyboardWillShow:(NSNotification *)notification
{
//这样就拿到了键盘的位置大小信息frame,然后根据frame进行高度处理之类的信息
CGRect frame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat endHeight = self.showScrollView.contentSize.height + frame.size.height;
self.showScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, endHeight);
self.showScrollView.contentOffset = CGPointMake(0, self.bottomView.originY);
}
/**
* 键盘将要隐藏
*
* @param notification 通知
*/
-(void)keyboardWillHidden:(NSNotification *)notification
{
CGRect frame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat endHeight = self.showScrollView.contentSize.height - frame.size.height;
self.showScrollView.contentSize = CGSizeMake(SCREEN_WIDTH, endHeight);
}
本文介绍如何在Swift中通过监听键盘显示与隐藏通知,调整滚动视图的高度,确保用户界面的平滑过渡。通过实现特定的观察者方法,获取键盘位置信息,动态调整滚动视图的尺寸,保持界面的可用性和用户体验。
4192

被折叠的 条评论
为什么被折叠?



