键盘通知和自定义键盘

1.键盘通知

键盘显示或消失时,系统会发送相关的通知

UIKeyboardWillShowNotification;
UIKeyboardDidShowNotification;
UIKeyboardWillHideNotification;
UIKeyboardDidHideNotification;

通知消息 NSNotification中的 userInfo字典中包含键盘的位置和大小信息,对应的key

// 包含CGRect结构,分别为键盘起始时和终止时的位置信息。
UIKeyboardFrameBeginUserInfoKey        NS_AVAILABLE_IOS(3_2); // NSValue of CGRect
UIKeyboardFrameEndUserInfoKey          NS_AVAILABLE_IOS(3_2); // NSValue of CGRect
// 表示键盘显示或消失时动画的持续时间
UIKeyboardAnimationDurationUserInfoKey NS_AVAILABLE_IOS(3_0); // NSNumber of double
// 表示键盘显示或消失的动画类型
UIKeyboardAnimationCurveUserInfoKey    NS_AVAILABLE_IOS(3_0); // NSNumber of NSUInteger (UIViewAnimationCurve)
UIKeyboardIsLocalUserInfoKey           NS_AVAILABLE_IOS(9_0); // NSNumber of BOOL
使用例子
// 注册处理键盘通知的方法
// 监听键盘 出现
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasChange:) name:UIKeyboardWillShowNotification object:nil];
// 隐藏
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
// 处理的事件
- (void)keyboardWasChange:(NSNotification *)aNotification {
    NSDictionary *info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
}
// 隐藏键盘
- (void)keyboardHide:(UITapGestureRecognizer *)tap
{
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder)
                                               to:nil
                                             from:nil
                                         forEvent:nil];
}

2.自定义键盘

系统键盘

1.keyboardType:键盘类型,如UIKeyboardTypeDefault,UIKeyboardTypeURL,UIKeyboardTypeEmailAddress,UIKeyboardTypePhonePad等
2.returnKeyType:键盘Return键显示的文本,默认为”Return”,其他可选择的包括Go,Next,Done,Send,Google等
3.keyboardAppearance:键盘外观,默认为 UIKeyboardAppearanceDefault,即上图中的浅兰色不透明背景,另外还有一种为 UIKeyboardAppearanceAlert,
键盘背景为黑色半透明,用于在警告框输入时显示
4.enablesReturnKeyAutomatically:是否在无文本时禁用Return键,默认为NO。若为YES,则用户至少输入一个字符后Return键才被激活
5.secureTextEntry:若输入的是密码,可设置此类型为YES,输入字符时可显示最后一个字符,其他字符显示为点

自己做键盘

推荐一个库 https://github.com/n-b/UIView-NibLoading
KeyboardView.h

#import "UIView+NibLoading.h"

@protocol KeyboardViewDelegate <NSObject>

- (void)keyboardBtnClick:(NSString *)title;

@end

@interface KeyboardView : NibLoadedView

@property (nonatomic, copy) NSString * type;
@property (nonatomic, weak) id<KeyboardViewDelegate> delegate;

@end

KeyboardView.m

#import "KeyboardView.h"

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

#define FIT_750_HEIGHT (SCREEN_HEIGHT/667.0)
#define FIT_750_WIDTH (SCREEN_WIDTH/375.0)

@interface KeyboardView ()

// 375
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *hiddenBtnHeight;     // 28
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *hiddenBtnWidth;      // 53

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *numBtnHeight;    // 73
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *numBtnWidth;     // 106

@property (strong, nonatomic) IBOutlet UIButton *okBtn;

@end

@implementation KeyboardView

@synthesize delegate;

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.hiddenBtnHeight.constant = FIT_750_HEIGHT * 28;
        self.hiddenBtnWidth.constant = FIT_750_WIDTH * 53;
        self.numBtnHeight.constant = FIT_750_HEIGHT * 73;
        self.numBtnWidth.constant = FIT_750_WIDTH * 106;
    }
    return self;
}

- (IBAction)hiddenBtnClick:(id)sender {
    if (delegate && [delegate respondsToSelector:@selector(keyboardBtnClick:)]) {
        [delegate keyboardBtnClick:@"hidden"];
    }
}
- (IBAction)num1BtnClick:(id)sender {
    if (delegate && [delegate respondsToSelector:@selector(keyboardBtnClick:)]) {
        [delegate keyboardBtnClick:@"1"];
    }
}
- (IBAction)num2BtnClick:(id)sender {
    if (delegate && [delegate respondsToSelector:@selector(keyboardBtnClick:)]) {
        [delegate keyboardBtnClick:@"2"];
    }
}
- (IBAction)num3BtnClick:(id)sender {
    if (delegate && [delegate respondsToSelector:@selector(keyboardBtnClick:)]) {
        [delegate keyboardBtnClick:@"3"];
    }
}
- (IBAction)num4BtnClick:(id)sender {
    if (delegate && [delegate respondsToSelector:@selector(keyboardBtnClick:)]) {
        [delegate keyboardBtnClick:@"4"];
    }
}
- (IBAction)num5BtnClick:(id)sender {
    if (delegate && [delegate respondsToSelector:@selector(keyboardBtnClick:)]) {
        [delegate keyboardBtnClick:@"5"];
    }
}
- (IBAction)num6BtnClick:(id)sender {
    if (delegate && [delegate respondsToSelector:@selector(keyboardBtnClick:)]) {
        [delegate keyboardBtnClick:@"6"];
    }
}
- (IBAction)num7BtnClcik:(id)sender {
    if (delegate && [delegate respondsToSelector:@selector(keyboardBtnClick:)]) {
        [delegate keyboardBtnClick:@"7"];
    }
}
- (IBAction)num8BtnClick:(id)sender {
    if (delegate && [delegate respondsToSelector:@selector(keyboardBtnClick:)]) {
        [delegate keyboardBtnClick:@"8"];
    }
}
- (IBAction)num9BtnClick:(id)sender {
    if (delegate && [delegate respondsToSelector:@selector(keyboardBtnClick:)]) {
        [delegate keyboardBtnClick:@"9"];
    }
}
- (IBAction)num0BtnClick:(id)sender {
    if (delegate && [delegate respondsToSelector:@selector(keyboardBtnClick:)]) {
        [delegate keyboardBtnClick:@"0"];
    }
}
- (IBAction)numCancelBtnClick:(id)sender {
    if (delegate && [delegate respondsToSelector:@selector(keyboardBtnClick:)]) {
        [delegate keyboardBtnClick:@"back"];
    }
}
- (IBAction)numOKBtnClick:(id)sender {
    if (delegate && [delegate respondsToSelector:@selector(keyboardBtnClick:)]) {
        [delegate keyboardBtnClick:@"ok"];
    }
}
@end

KeyboardView.xib
约束条件按照键盘要求设置

在使用的文件中,初始化键盘视图(alloc initWithFrame…),设置代理遵守协议,实现协议方法,并且替代系统键盘

// 初始化
KeyBoardView * myKeyBoard = [[MyBoardView alloc] initWithFrame:...];
myKeyBoard.delegate = self;
UITextField * myTextField = [UITextField ...]...;
myTextField.inputView = myKeyBoard;
// 协议方法

- (void)keyboardBtnClick:(NSString *)title {

    if ([title isEqualToString:@"hidden"]) {

    } else if ([title isEqualToString:@"1"]) {

    } else if ([title isEqualToString:@"2"]) {

    } else if ([title isEqualToString:@"3"]) {

    } else if ([title isEqualToString:@"4"]) {

    } else if ([title isEqualToString:@"5"]) {

    } else if ([title isEqualToString:@"6"]) {

    } else if ([title isEqualToString:@"7"]) {

    } else if ([title isEqualToString:@"8"]) {

    } else if ([title isEqualToString:@"9"]) {

    } else if ([title isEqualToString:@"0"]) {

    } else if ([title isEqualToString:@"back"]) {

    } else if ([title isEqualToString:@"ok"]) {

    }

}

你可能感兴趣的:(键盘,通知)