IOS5 Perfect solution for overwriting input box when switching Chinese keyboard

Source: Internet
Author: User

iOS5 Perfect solution for overwriting input box when switching Chinese keyboard

Original: http://wangsheng2008love.blog.163.com/blog/static/782016892012631102714562/

2012-07-31 22:42:43|  Category: IOS | Tags:ios5 chinese keyboard masking input Box | Report | Font size Subscription

As we all know, before the iOS5, the iphone keyboard height is fixed to 216.0px high, the Chinese character selection box is suspended, so many applications will be this height to mark the height of the keyboard (including rice chat is also done).

However, in the iOS5, the keyboard layout has changed, especially Chinese input, the Chinese character selection box is fixed on the keyboard, so that the original interface with the keyboard close to the view by the Chinese character selection box to cover. On the one hand, it affects the aesthetics of the interface, on the other hand, if the part that is covered is the text input box, the user cannot see the input content. So the problem has to be solved.

Workaround :

In fact, in the beginning to use the fixed value of 216.0px to label the height of the keyboard is wrong. Because in the system after iOS3.2, Apple provides the API that the keyboard uses as well as the demo program-"Keyboardaccessory".

The correct way to handle keyboard events is this: (including getting the location of the keyboard and the time of the keyboard popping and disappearing animations)

  1) Receive notification of keyboard events in the View controller (both viewdidload) to use the keyboard:

[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyboardwillshow:) Name: Uikeyboardwillshownotification Object:nil];
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyboardwillhide:) Name: Uikeyboardwillhidenotification Object:nil];

Keyboard height change notification, ios5.0 new
#ifdef __IPHONE_5_0
float Version = [[[Uidevice Currentdevice] systemversion] floatvalue];
if (version >= 5.0) {
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (keyboardwillshow:) Name: Uikeyboardwillchangeframenotification Object:nil];
}
#endif

2) then add the processing code for the keyboard event:

Gets the height of the current keyboard, as well as the animation time, and then the corresponding action on the view.

#pragma mark-
#pragma mark Responding to keyboard events
-(void) Keyboardwillshow: (nsnotification *) Notification {
/*

Reduce the size of the text view so that it's not a obscured by the keyboard.

Animate The resize so, it's in sync with the appearance of the keyboard.

*/

Nsdictionary *userinfo = [notification UserInfo];

Get The origin of the keyboard when it ' s displayed.

Nsvalue *avalue = [UserInfo Objectforkey:uikeyboardframeenduserinfokey];

Get the top of the keyboard as the Y coordinate of its origin in Self's view ' s coordinate system. The bottom of the text view ' s frame should align with the top of the keyboard ' s final position.

CGRect keyboardrect = [Avalue cgrectvalue];

Get the duration of the animation.

Nsvalue *animationdurationvalue = [Userinfoobjectforkey:uikeyboardanimationdurationuserinfokey];

Nstimeinterval animationduration;

[Animationdurationvalue getvalue:&animationduration];

Animate The resize of the text view ' s frame in sync with the keyboard ' s appearance.

[UIView animatewithduration:animationduration animations:^{

The viewfooter here is your input box view

20 is the height of the status bar

Self.viewFooter.frame = CGRectMake (viewfooter.frame.origin.x, Keyboardrect.origin.y-20-viewfooter.frame.size.height,viewfooter.frame.size.width, ViewFooter.frame.size.height );

} completion:^ (BOOL finished) {

}];

}


-(void) Keyboardwillhide: (nsnotification *) Notification {
nsdictionary* userInfo = [notification UserInfo];

nsvalue* avalue = [UserInfo Objectforkey:uikeyboardframeenduserinfokey];

CGRect keyboardrect = [Avalue cgrectvalue];

/*

Restore the size of the text view (fill self ' s view).

Animate The resize so, it's in sync with the disappearance of the keyboard.

*/

[UIView animatewithduration:0 animations:^{

Self.viewFooter.frame = CGRectMake (viewfooter.frame.origin.x, Keyboardrect.origin.y-20-viewfooter.frame.size.height,viewfooter.frame.size.width, ViewFooter.frame.size.height );

} completion:^ (BOOL finished) {

}];

}

  3) Remove the notification of the keyboard event when the view controller is eliminated (that is, viewdidunload):

[[Nsnotificationcenter Defaultcenter] removeobserver:self];

On a multiple input interface, there will be multiple input boxes of type Uitextfield. For scrolling, we'll add them to the UITableView cell, forming a data entry interface that can slide up and down.

But the character input is done by the system to automatically eject the soft keyboard, so when the cell at the bottom of the screen is selected, it is covered by the area of the soft keyboard.

Again, because the input box is in the UITableView cell, you can scroll the cell where it is located above the area covered by the soft keyboard. Allows us to see the input box.

Now, we're going to implement this procedure.

We can simply scroll the cell to the topmost position in the visible area of the table view.

UITableView provides this method "scrolltorowatindexpath:atscrollposition:animated:". As the first reactant input box, it can delegate method-(void) textfielddidbeginediting: (uitextfield*) TextField

Assign it to a variable in Lastedittextfield. The appearance and disappearance of the soft keyboard are registered to the view Controller class and implement its methods. In this way, a soft keyboard popup notification is automatically submitted when the Uitextfield activates the pop-up keyboard. A method is called by the keyboard popup notification.

The logic to implement this method is to get the cell of the operation according to the location point where the Lastedittextfield is located, and then scroll the cell to the top of the visible area of the view.

Cgpoint PT =lastedittextfield.center;

PT = [Self.tableview Convertpoint:ptfromview:lastedittextfield.superview];

nsindexpath* Indexpath = [SELF.TABLEVIEWINDEXPATHFORROWATPOINT:PT];

[Self.tableview scrolltorowatindexpath:indexpathatscrollposition:uitableviewrowanimationtop Animated:yes];

In this code, the most important is based on the value of Lastedittextfield.center, through the UIView instance Method-(Cgpoint) Convertpoint: (cgpoint) point fromview: (UIView *) View turns it into a value under the Self.tableview coordinate system. Then, according to the value of lastedittextfield.center in the new coordinate system, the example method of UITableView is scrolltorowatindexpath:atscrollposition:animated: Scroll the cell to the top of the screen.

When the keyboard disappears, a notification is sent to restore the screen to its original location.

Scrolls the input box to the top, although it satisfies our input visibility requirements. However, if you can only scroll to the top of the soft keyboard, not the top of the UITableView, you do not need to scroll when the input box is not covered by the keyboard. Such treatment would be a more satisfying outcome.

IOS5 Perfect solution for overwriting input box when switching Chinese keyboard

Related Article

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.