IOS NSNotification 键盘遮挡问题的解决办法
IOSNSNotification键盘遮挡问题的解决办法
从键盘通知中获得键盘尺寸
键盘尺寸存在于NSNotification中。
1;在AddDrinkViewController中添加keyboardDidShow和keyboardDidHide方法
2;在viewWillAppear中注册UIKeyboardDidshowNotification与UIKeyboardDidHideNotification。
3;在viewWillDisappear中取消对所有事件的订阅注册
4;在AddDrinkViewController中添加一个Bool成员,跟踪键盘是否可见的状态。
//
//ViewController.h
//scrol
//
//Createdbygaowuhangon12-12-5.
//Copyright(c)2012年gaowuhang.Allrightsreserved.
//
#import
@interfaceViewController:UIViewController{
BOOLkeyboardVisible;
UIScrollView*scrollView;
}
-(void)keyboardDidShow:(NSNotification*)notif;
-(void)keyboardDidHide:(NSNotification*)notif;
@property(nonatomic,retain)UIScrollView*scrollView;
@end
//
//ViewController.m
//scrol
//
//Createdbygaowuhangon12-12-5.
//Copyright(c)2012年gaowuhang.Allrightsreserved.
//
#import"ViewController.h"
@interfaceViewController()
@end
@implementationViewController
@synthesizescrollView;
-(void)viewWillAppear:(BOOL)animated{
[superviewWillAppear:animated];
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardDidShow:)name:UIKeyboardDidShowNotificationobject:nil];
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardDidHide:)name:UIKeyboardDidHideNotificationobject:nil];
}
-(void)viewWillDisappear:(BOOL)animated{
[[NSNotificationCenterdefaultCenter]removeObserver:self];
}
-(void)keyboardDidShow:(NSNotification*)notif{
NSLog(@"%@",@"ReceivedUIKeyboardDidShowNotification");
if(keyboardVisible){
NSLog(@"%@",@"Keyboardisalreadyvisible.Ignoringnotifications.");
return;
}
//Thekeyboardwasn'tvisiblebefore
NSLog(@"Resizingsmallerforkeyboard");
//Gettheoriginofthekeyboardwhenitfinishesanimating
NSDictionary*info=[notifuserInfo];
NSValue*aValue=[infoobjectForKey:UIKeyboardFrameEndUserInfoKey];
//Getthetopofthekeyboardinview'scoordinatesystem.
//Weneedtosetthebottomofthescrollviewtolineupwithit
CGRectkeyboardRect=[aValueCGRectValue];
keyboardRect=[self.viewconvertRect:keyboardRectfromView:nil];
CGFloatkeyboardTop=keyboardRect.origin.y;
//Resizethescrollviewtomakeroomforthekeyboard
CGRectviewFrame=self.view.bounds;
viewFrame.size.height=keyboardTop-self.view.bounds.origin.y;
self.scrollView.frame=viewFrame;
keyboardVisible=YES;
}
-(void)keyboardDidHide:(NSNotification*)notif{
NSLog(@"%@",@"ReceivedUIKeyboardDidHideNotification");
if(!keyboardVisible){
NSLog(@"%@",@"Keyboardalreadyhidden.Ignoringnotification.");
return;
}
//Thekeyboardwasvisible
NSLog(@"%@",@"Resizingbiggerwithnokeyboard");
//Resizethescrollviewbacktothefullsizeofourview
self.scrollView.frame=self.view.bounds;
keyboardVisible=NO;
}
-(void)viewDidLoad
{
scrollView=[[UIScrollViewalloc]initWithFrame:CGRectMake(0,0,320,460)];
//scroll.contentSize=CGSizeMake(1000,1000);
[self.viewaddSubview:scrollView];
//UIButton*button=[[UIButtonalloc]initWithFrame:CGRectMake(100,100,100,100)];
//[buttonsetBackgroundColor:[UIColorblackColor]];
//[scrolladdSubview:button];
UITextView*textView=[[UITextViewalloc]initWithFrame:CGRectMake(100,300,100,100)];
textView.text=@"222";
textView.font=[UIFontsystemFontOfSize:20];
[scrollViewaddSubview:textView];
[superviewDidLoad];
[textViewrelease];
self.scrollView.contentSize=self.view.frame.size;
//Doanyadditionalsetupafterloadingtheview,typicallyfromanib.
}
-(void)dealloc
{
[scrollViewrelease];
[superdealloc];
}
-(void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
//Disposeofanyresourcesthatcanberecreated.
}
@end
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!