iOS 基于键盘的编程

首先,你得监听键盘的事件,最基本的两个事件:

UIKeyboardWillShowNotification

UIKeyboardWillHideNotification

UIKeyboard...

iOS 5新增加了一些

UIKeyboardDidChangeFrameNotification(will)

一般情况下,前两个事件已经可以完成你要做的事情。在你的事件处理方法中加上NSNotification参数可以为你获得更多的东西:

view plainprint?

- (void)keyboardWillShow:(NSNotification *)notification 

{  

    CGPoint beginCentre = [[[notification userInfo] valueForKey:UIKeyboardCenterBeginUserInfoKey] CGPointValue]; 

    CGPoint endCentre = [[[notification userInfo] valueForKey:UIKeyboardCenterEndUserInfoKey] CGPointValue]; 

    CGRect keyboardBounds = [[[notification userInfo] valueForKey:UIKeyboardBoundsUserInfoKey] CGRectValue]; 

    CGRect keyboardFrames = [[[notification userInfo] valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; 

    UIViewAnimationCurve animationCurve = [[[notification userInfo] valueForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]; 

    NSTimeInterval animationDuration = [[[notification userInfo] valueForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 

 如果你要在程序中使用键盘的高度和宽度,永远不要尝试去手动指定,动态获取也很简单而且一定准确,不会出现键盘

挡住输入框的问题。

你可以利用这些参数把动画做的和键盘一致。假设你要把一个控件放在Window上,并且想让它的交互方式和键盘一样,

如果只是简单的做个向下偏移动画并不能很好的完成,因为你还要考虑从导航栏中Pop出来的时候,这个时候的键盘动画是

在x轴上偏移的,你用UIKeyboardFrameEndUserInfoKey获取的frame可以很准确的做到。

 

如果在某些特殊的字段上,你不想用默认的键盘,而是用类似于Picker这样的拾取器,你只需要设置inputView就行了,用你自定义的视图去替换掉键盘;如果你想在键盘上面再增加一个视图,比如toolbar,那么你可以不用自己对toolbar的位置进行控制,只需要设置inputAccessoryView就行了,这个值默认为nil,设置的视图将在你的控件变成第一响应者的时候显示在inputView的上方。

\\

 

在UIScrollView(UITableView继承于它)上,当你触碰控件使之变成第一响应者的时候,系统会自动调整位置,避免键盘挡住控件。如果在代码中用becomeFirstResponder使之变成第一响应者将不会出现自动调整。你可以设置contentOffset去手动调整。其他视图,最简单的方法就是修改控件的frame属性,让控件总是显示在可见区域



常见通知:

UIKIT_EXTERN NSString *const UIWindowDidBecomeVisibleNotification; // nil
UIKIT_EXTERN NSString *const UIWindowDidBecomeHiddenNotification;  // nil
UIKIT_EXTERN NSString *const UIWindowDidBecomeKeyNotification;     // nil
UIKIT_EXTERN NSString *const UIWindowDidResignKeyNotification;     // nil

// Each notification includes a nil object and a userInfo dictionary containing the
// begining and ending keyboard frame in screen coordinates. Use the various UIView and
// UIWindow convertRect facilities to get the frame in the desired coordinate system.
// Animation key/value pairs are only available for the "will" family of notification.
UIKIT_EXTERN NSString *const UIKeyboardWillShowNotification;
UIKIT_EXTERN NSString *const UIKeyboardDidShowNotification;
UIKIT_EXTERN NSString *const UIKeyboardWillHideNotification;
UIKIT_EXTERN NSString *const UIKeyboardDidHideNotification;

UIKIT_EXTERN NSString *const UIKeyboardFrameBeginUserInfoKey        NS_AVAILABLE_IOS(3_2); // NSValue of CGRect
UIKIT_EXTERN NSString *const UIKeyboardFrameEndUserInfoKey          NS_AVAILABLE_IOS(3_2); // NSValue of CGRect
UIKIT_EXTERN NSString *const UIKeyboardAnimationDurationUserInfoKey NS_AVAILABLE_IOS(3_0); // NSNumber of double
UIKIT_EXTERN NSString *const UIKeyboardAnimationCurveUserInfoKey    NS_AVAILABLE_IOS(3_0); // NSNumber of NSUInteger (UIViewAnimationCurve)

// Like the standard keyboard notifications above, these additional notifications include
// a nil object and begin/end frames of the keyboard in screen coordinates in the userInfo dictionary.
UIKIT_EXTERN NSString *const UIKeyboardWillChangeFrameNotification  NS_AVAILABLE_IOS(5_0);
UIKIT_EXTERN NSString *const UIKeyboardDidChangeFrameNotification   NS_AVAILABLE_IOS(5_0);

// These keys are superseded by UIKeyboardFrameBeginUserInfoKey and UIKeyboardFrameEndUserInfoKey.
UIKIT_EXTERN NSString *const UIKeyboardCenterBeginUserInfoKey   NS_DEPRECATED_IOS(2_0, 3_2);
UIKIT_EXTERN NSString *const UIKeyboardCenterEndUserInfoKey     NS_DEPRECATED_IOS(2_0, 3_2);
UIKIT_EXTERN NSString *const UIKeyboardBoundsUserInfoKey        NS_DEPRECATED_IOS(2_0, 3_2);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值