Keyboard, In-call Status Bar monitoring and view position change adjustment (example description)

Source: Internet
Author: User

Written in front:
When there is a TextField, when the keyboard appears, need to change the frame of the other view, as well as the appearance of the In-call Status bar will also affect other views of the frame, then through the notification mode to observe the changes, and when it changes to do some action.

Code ideas: (View frame adjustment, to create a simple toolbar as an example to achieve)
1, the creation of the bottom column, where TextField set up the agent, follow
2,uitextfielddelegate protocol, set click Return to return to the keyboard disappears
3. Registration of Notices
4, remove the notification, remove in the Viewwilldisappear function
5, showkeyboard,hidekeyboard,statusbarchange respectively to monitor the function after the change, this example modifies the bottom frame, other operations can also be done here, this example of the setting, Can implement any combination of StatusBar and keyboard, without affecting the bottom view location
(At the end)

#import "ViewController.h" #define Kwidth [UIScreen mainscreen].bounds.size.width#define Kheight [UIScreen mainscreen].bounds.size.height#define Kbottom @interface viewcontroller (){UIView*_bottomview;}@end @implementation viewcontroller - (void) Viewdidload {[SuperViewdidload]; Self. View. BackgroundColor= [UicolorGraycolor]; [ Self_creatbottombar];//Create a bottom bar[ Self_notificationset];//Notification registration}
- (void) _notificationset {//Monitor keyboard events--Appears[[NsnotificationcenterDefaultcenter]addobserver: SelfSelector@selector(Showkeyboard:) Name:uikeyboardwillshownotification object:Nil];//Monitor keyboard events--disappear[[NsnotificationcenterDefaultcenter]addobserver: SelfSelector@selector(Hidekeyboard:) Name:uikeyboarddidhidenotification object:Nil];//monitor change of status bar[[NsnotificationcenterDefaultcenter]addobserver: SelfSelector@selector(Statusbarchange:) Name:uiapplicationwillchangestatusbarframenotification object:Nil];}
- (void) _creatbottombar {//self-customization_bottomview = [[UIViewAlloc] Initwithframe:cgrectmake (0, Kheight-kbottom, Kwidth, Kbottom)]; _bottomview. BackgroundColor= [UicolorDarkgraycolor];//Input BoxUitextfield *textfield = [[Uitextfield alloc] Initwithframe:cgrectmake (Ten,5, kwidth- -, +)]; TextField. Background= [UIImageimagenamed:@"Chat_bottom_textfield"];//Set up the agent to follow the protocol uitextfielddelegateTextField. Delegate= Self;    [_bottomview Addsubview:textfield]; [ Self. ViewAddsubview:_bottomview];}

What to do when the keyboard appears

- (void)showKeyBoard:(NSNotification *)notification {//    NSLog(@"%@",notification.userInfo);     //可以打印看看里面有哪些细节    NSValue *rectValue = [notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];    CGRect keyboardRect = [rectValue CGRectValue];    CGRect keyboardFrame = [self.view convertRect:keyboardRect fromView:[[UIApplication sharedApplication] keyWindow]];    CGFloat keyboardHeight = keyboardFrame.size.height;    //记录前一次的bottomView的位置    CGFloat lastBottomY = _bottomView.frame.origin.y;    //调整frame    _bottomView.frame = CGRectMake(0, lastBottomY-keyboardHeight, kwidth, kbottom);}

What to do when the keyboard is hidden:

- (void)hideKeyBoard:(NSNotification *)notification {    NSValue *rectValue = [notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];    CGRect keyboardRect = [rectValue CGRectValue];    CGRect keyboardFrame = [self.view convertRect:keyboardRect fromView:[[UIApplication sharedApplication] keyWindow]];    CGFloat keyboardHeight = keyboardFrame.size.height;    CGFloat lastBottomY = _bottomView.frame.origin.y;    _bottomView.frame = CGRectMake(0, lastBottomY+keyboardHeight, kwidth, kbottom);}

In-call Status Bar Changes:

-(void) Statusbarchange: (nsnotification *) Notification {//Get change parameters    Nsvalue *rectvalue = [Notification.userinfo Objectforkey:uiapplicationstatusbarframeuserinfokey];    CGRect statusrect = [Rectvalue cgrectvalue]; CGRect statusframe = [Self.view convertrect:statusrect fromview:[[UIApplication    Sharedapplication]keywindow]] ;    CGFloat statusheight = Statusframe.size.height-20 ;    Modify CGFloat lastbottomy = _BOTTOMVIEW.FRAME.ORIGIN.Y; if  (Statusheight = = 0 ) {_bottomview.frame = CGRectMake (0 , Lastbottomy+20, Kwidth, Kbottom); }else  {_bottomview.frame = CGRectMake (0 , LastBo    Ttomy-statusheight, Kwidth, Kbottom); }}

Viewer removal

- (void)viewWillDisappear:(BOOL)animated {    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillChangeStatusBarFrameNotification object:nil];    NSLog(@"解除通知");}

Click the Keyboard return button and the keyboard disappears

- (BOOL)textFieldShouldReturn:(UITextField *)textField {    [textField resignFirstResponder];    returnYES;}

: (All combinations of all keyboard and In-call Satus Bar are implemented normally)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Keyboard, In-call Status Bar monitoring and view position change adjustment (example description)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.