以下是我在用户开始输入textview时为移动textview编写的一段代码。
这是代码,
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView{
if(textView == _textViewMsg || textView == _subjectView ){
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
// return YES;
}
return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
if(textView == _textViewMsg || textView == _subjectView ){
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
[self.view endEditing:YES];
}
return YES;
}
- (void)keyboardDidShow:(NSNotification *)notification
{
[self.view setFrame:CGRectMake(0,-50,320,460)];
}
-(void)keyboardDidHide:(NSNotification *)notification
{
[self.view setFrame:CGRectMake(0,60,320,520)];
}
当我在移动中单击textview(在主题中)视图时,我可以输入textview。它工作正常。 (图1)
当我点击完成按钮时,隐藏,即在键盘隐藏时,一个黑色视图即将到来(检查图像2)几秒钟,然后再次正常显示。
编辑:
解决方案:@michael刚刚给我解决方案
将UIKeyboardDidHideNotification
更改为UIKeyboardWillHideNotification
它对我有用。
发生新问题: 当我开始输入前2个文本视图,即Requester,firstname& lastname ...我无法输入它,因为它正在向上移动。
答案 0 :(得分:1)
添加观察者应该在viewdidload中。尝试将添加键盘观察者的代码移动到viewdidload。
修改强>
而不是...通知类型检查将... 所以用UIKeyboardWillHide替换UIKeyboardDidShowNotification和用UIKeyboardWillHide替换UIKeyboardDidHideNotification。
答案 1 :(得分:1)
之所以发生这种情况,是因为在键盘确实消失后,系统动画完成后更改了视图的帧。快速修复:将UIKeyboardDidHideNotification
更改为UIKeyboardWillHideNotification
但是我想在你的代码中指出其他一些项目,如果你不介意的话。
<强> 1 强>
您每次开始编辑时都订阅通知。这意味着当用户第二次开始输入时,您的代码将被触发两次。订阅viewDidAppear
上的通知并取消订阅viewWillDisappear
更为合适:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear: animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardDidHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
<强> 2 强> 然后将帧更改为常量值。但现在苹果支持不同尺寸的不同键盘,更不用说键盘尺寸因不同设备和不同可能模式而异。因此,从通知中获取键盘的大小要明智得多:
- (void)keyboardDidShow:(NSNotification *)aNotification
{
CGRect keyboardFrame = [[aNotification.userInfo valueForKey: UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect overlap = CGRectIntersection( self.view.frame, keyboardFrame);
// Setup new frmae according to overlap, for example reduce size by half of overlap, and move up by another half
CGRect newFrame = self.view.frame;
newFrame.origin.y -= overlap.size.height / 2.0
newFrame.size.height -= overlap.size.height / 2.0
self.view.frame = newFrame;
}
第3:强>
动画:您也可以从通知中获取键盘动画持续时间:
UIKeyboardAnimationDurationUserInfoKey
:
NSTimeInterval duration = [[aNotification.userInfo valueForKey: UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[UIView animateWithDuration:duration animations:^{
self.view.frame = newFrame;
}];
<强> 4 强> 由于您似乎使用滚动视图(UITableView和UICollectioView是滚动视图),您可以代替更改框架,更改内容插入(将空白空间添加到底部滚动视图)
[self.view setContentInset:UIEdgeInsetsMake(0, 0, overlap.size.height, 0)];
<强> 5 强> 考虑使用AutoLayout。
答案 2 :(得分:0)
使用IQKeyboardManager框架将有助于处理键盘隐藏和取消隐藏问题,
键盘处理的最有效方式:IQKeyboardManager
方法1: 您可以将Cocoa Pod文件用于IQKeyboardManager库。
方法2:
从github下载IQKeyboardManger SDK并拖放放入项目文件中。
Swift代码段:
导入IQKeyboardManagerSwift
@UIApplicationMain class AppDelegate:UIResponder,UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
IQKeyboardManager.sharedManager().enable = true
return true
}
}
Objective-C片段:
#import <IQKeyboardManager/IQKeyboardManager.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//ONE LINE OF CODE.
//Enabling keyboard manager(Use this line to enable managing distance between keyboard & textField/textView).
[[IQKeyboardManager sharedManager] setEnable:YES];
return true;
}
希望这可以帮助您解决键盘隐藏和取消隐藏UI问题。