iOS add control bar to keyboard

Source: Internet
Author: User

Keyboard is used frequently in iOS, sometimes adding a control bar to the keyboard can be easily and quickly switch or hide in different input boxes

Here is a simple way to implement

Initializes a uitoolbar and adds it to the interface, dynamically changing as the keyboard's height changes to show

Here's a look at the code implementation

Header file Section

#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interface uikeyboardtool:nsobject/// Toolbar@property for Interface display (Nonatomic,strong) Uitoolbar *toolbar;///array for cursor switching @property (nonatomic,strong) NSArray * fieldarray;///display keyboard-(void) ShowToolbar: (Uitextfield *) field; @end

Initialization

-(Instancetype) init{if (self = = [Super init]) {cgfloat screenheight = [[UIScreen mainscreen] Bounds].si        Ze.height;                CGFloat screenwidth = [[UIScreen mainscreen] bounds].size.width;                        ToolBar = [[Uitoolbar alloc] Initwithframe:cgrectmake (0, ScreenHeight, ScreenWidth, 44)]; Uibarbuttonitem *nextitem = [[Uibarbuttonitem alloc] initwithtitle:@ "Next" style: (uibarbuttonitemstyleplain) Target:        Self action: @selector (Shownext)]; Uibarbuttonitem *previousitem = [[Uibarbuttonitem alloc] initwithtitle:@ "Previous" style: (Uibarbuttonitemstyleplain)        Target:self Action: @selector (showprevious)]; Uibarbuttonitem *spaceitem = [[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem: (        Uibarbuttonsystemitemflexiblespace) target:self Action:nil]; Uibarbuttonitem *doneitem = [[Uibarbuttonitem alloc] initwithtitle:@ "Hide" style: (uibarbuttonitemstyleplain) Target:                Self action: @selector (showhide)]; Toolbar.items = @[nextitem,previoUsitem,spaceitem,doneitem]; Monitor keyboard height changes [[nsnotificationcenter defaultcenter] addobserver:self selector: @selector (keyboardframechange:) name:ui        Keyboardwillchangeframenotification Object:nil];            Currentfield = nil; } return self;}

Keyboard display

Show keyboard-(void) ShowToolbar: (Uitextfield *) field{    currentfield = field;        [UIView Beginanimations:nil Context:nil];    [UIView setanimationduration:0.25];    CGFloat screenheight = [[UIScreen mainscreen] bounds].size.height;    CGFloat screenwidth = [[UIScreen mainscreen] bounds].size.width;    [ToolBar setframe:cgrectmake (0, screenHeight-300, ScreenWidth,)];    [UIView commitanimations];}

Input box Toggle

Click Next-(void) shownext{    nsinteger current = [Fieldarray Indexofobject:currentfield];    if (current + 1) < Fieldarray.count)    {        Uitextfield *field = [Fieldarray objectatindex:current + 1];        [Field Becomefirstresponder];}    }

With iOS update, iOS keyboard height is not 216 in iOS5, but there are many situations, so by listening to system events
Uikeyboardwillchangeframenotification

To dynamically handle height changes, the value returned by the notification is a nsdictionary

The values in this are as follows

Animation curve Type Uikeyboardanimationcurveuserinfokey = 7;///Animation Duration Uikeyboardanimationdurationuserinfokey = "0.25"; Uikeyboardboundsuserinfokey = "Nsrect: {{0, 0}, {375, 258}}";///The center point at the beginning of the keyboard animation Uikeyboardcenterbeginuserinfokey = "Nspoint : {187.5, 796} ";///The center point at the end of the keyboard animation Uikeyboardcenterenduserinfokey =" Nspoint: {187.5, 538} ";/// The size of the keyboard animation starts at uikeyboardframebeginuserinfokey = "Nsrect: {{0, 667}, {375, 258}}";/// The size of the keyboard animation at the end of Uikeyboardframeenduserinfokey = "Nsrect: {{0, 409}, {375, 258}}";///whether the keyboard is a display, bool type, 1 display, 2 Hidden uikeyboardislocaluserinfokey = 1;

Judging by the last bool value

Dynamic change frame-(void) Keyboardframechange: (nsnotification *) notify{    nsdictionary *dict = [notify UserInfo];    Nsvalue *endvalue = [Dict objectforkey:uikeyboardframeenduserinfokey];        CGRect endframe = [Endvalue cgrectvalue];            [UIView Beginanimations:nil Context:nil];    [UIView setanimationduration:0.25];    CGFloat screenheight = [[UIScreen mainscreen] bounds].size.height;    CGFloat screenwidth = [[UIScreen mainscreen] bounds].size.width;     NSNumber *isshowkeyboardvalue = [dict objectforkey:@ "Uikeyboardislocaluserinfokey"];    BOOL isshowkeyboard = isshowkeyboardvalue.boolvalue;            if (Isshowkeyboard) {///        Keyboard height Change        [ToolBar setframe:cgrectmake (0, endframe.origin.y-44, ScreenWidth, )];    }    else    {        ///keyboard Hide [ToolBar setframe:cgrectmake (0, ScreenHeight, ScreenWidth,)];    }        [UIView commitanimations];}

How to use

Initialization

    tool = [[Uikeyboardtool alloc] init];    Tool.fieldarray = @[field,field1,field2];    [Self.view AddSubview:tool.toolBar];

Show

-(void) textfielddidbeginediting: (nonnull Uitextfield *) textfield{    [Tool Showtoolbar:textfield];}

Problems encountered, with the iOS9 update, when the word Lenovo interface, the previous version of the background is opaque, but the iOS9 becomes transparent, so the result will be as follows, and this time the control bar is not clickable, the British department that beautiful


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

iOS add control bar to keyboard

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.