IOS NSNotification 键盘遮挡问题的解决办法
程序员文章站
2023-12-20 16:07:52
ios nsnotification 键盘遮挡问题的解决办法
从键盘通知中获得键盘尺寸
键盘尺寸存在于nsnotification中。
1;在adddrinkvi...
ios nsnotification 键盘遮挡问题的解决办法
从键盘通知中获得键盘尺寸
键盘尺寸存在于nsnotification中。
1;在adddrinkviewcontroller中添加keyboarddidshow和keyboarddidhide方法
2;在viewwillappear中注册uikeyboarddidshownotification与uikeyboarddidhidenotification。
3;在viewwilldisappear中取消对所有事件的订阅注册
4;在adddrinkviewcontroller中添加一个bool成员,跟踪键盘是否可见的状态。
// // viewcontroller.h // scrol // // created by gao wuhang on 12-12-5. // copyright (c) 2012年 gao wuhang. all rights reserved. // #import @interface viewcontroller : uiviewcontroller{ bool keyboardvisible; uiscrollview *scrollview; } - (void)keyboarddidshow: (nsnotification*) notif; - (void)keyboarddidhide: (nsnotification*) notif; @property (nonatomic, retain) uiscrollview *scrollview; @end // // viewcontroller.m // scrol // // created by gao wuhang on 12-12-5. // copyright (c) 2012年 gao wuhang. all rights reserved. // #import "viewcontroller.h" @interface viewcontroller () @end @implementation viewcontroller @synthesize scrollview; - (void)viewwillappear:(bool)animated{ [super viewwillappear:animated]; [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(keyboarddidshow:) name:uikeyboarddidshownotification object:nil]; [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(keyboarddidhide:) name:uikeyboarddidhidenotification object:nil]; } - (void)viewwilldisappear:(bool)animated{ [[nsnotificationcenter defaultcenter] removeobserver:self]; } - (void) keyboarddidshow:(nsnotification *)notif { nslog(@"%@", @"received uikeyboarddidshownotification"); if (keyboardvisible) { nslog(@"%@", @"keyboard is already visible. ignoring notifications."); return; } // the keyboard wasn't visible before nslog(@"resizing smaller for keyboard"); // get the origin of the keyboard when it finishes animating nsdictionary *info = [notif userinfo]; nsvalue *avalue = [info objectforkey:uikeyboardframeenduserinfokey]; // get the top of the keyboard in view's coordinate system. // we need to set the bottom of the scrollview to line up with it cgrect keyboardrect = [avalue cgrectvalue]; keyboardrect = [self.view convertrect:keyboardrect fromview:nil]; cgfloat keyboardtop = keyboardrect.origin.y; // resize the scroll view to make room for the keyboard cgrect viewframe = self.view.bounds; viewframe.size.height = keyboardtop - self.view.bounds.origin.y; self.scrollview.frame = viewframe; keyboardvisible = yes; } - (void) keyboarddidhide:(nsnotification *)notif { nslog(@"%@", @"received uikeyboarddidhidenotification"); if (!keyboardvisible) { nslog(@"%@", @"keyboard already hidden. ignoring notification."); return; } // the keyboard was visible nslog(@"%@", @"resizing bigger with no keyboard"); // resize the scroll view back to the full size of our view self.scrollview.frame = self.view.bounds; keyboardvisible = no; } - (void)viewdidload { scrollview = [[uiscrollview alloc] initwithframe:cgrectmake(0, 0, 320, 460)]; // scroll.contentsize = cgsizemake(1000, 1000); [self.view addsubview:scrollview]; // uibutton *button = [[uibutton alloc] initwithframe:cgrectmake(100, 100, 100, 100)]; // [button setbackgroundcolor:[uicolor blackcolor]]; // [scroll addsubview:button]; uitextview *textview = [[uitextview alloc]initwithframe:cgrectmake(100, 300, 100, 100)]; textview.text = @"222"; textview.font = [uifont systemfontofsize:20]; [scrollview addsubview:textview]; [super viewdidload]; [textview release]; self.scrollview.contentsize = self.view.frame.size; // do any additional setup after loading the view, typically from a nib. } - (void)dealloc { [scrollview release]; [super dealloc]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of any resources that can be recreated. } @end
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!