在为UITextField分配值时,UIView向下移动

时间:2016-02-04 08:18:32

标签: ios objective-c iphone uiview

我正在创建一个应用程序,当显示键盘时,移动视图(当iPone 4s / 5 / 5c / 5s时)。但是,当我使用数字pa键入内容时,它会触发视图返回。 public MyContext() : base("MyConnection") { var objectContext = ((IObjectContextAdapter) this).ObjectContext; objectContext.SavingChanges += (sender, args) => { foreach (var entry in ChangeTracker.Entries()) { var entity = entry.Entity; var state = entry.State; if (entity is BaseEntity) { ProcessEntity((BaseEntity)entity, state); } } ChangeTracker.DetectChanges(); }; } private void ProcessEntity<T>(T entity, EntityState state) where T : BaseEntity { switch (state) { case EntityState.Added: var set = Set<T>(); (entity as BaseEntity).Id = GetMaxId(set) + 1; break; } } 的代码会更改文本,然后将其分配回教科书。

textField:shouldChangeCharactersInRange:replacementString

我与- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSString* totalString = [NSString stringWithFormat:@"%@%@",textField.text,string]; if(textField.tag==102) { if (([textField.text isEqualToString:@"0"]) && (textField.text.length == 1)) { textField.text = @""; NSLog(@"Zero Encountered"); return NO; } if (range.length == 1) { // Delete button was hit.. so tell the method to delete the last char. textField.text = [_iOSApiService_model formatPhoneNumber:totalString deleteLastChar:YES]; } else { textField.text = [_iOSApiService_model formatPhoneNumber:totalString deleteLastChar:NO]; } return YES; } return YES; } -(void) keyboardWasShown:(NSNotification *)notification { //The keyboard was shown, move movableView objects UP NSLog(@"value of _keyboardshownUp is %i",_keyboardshownUp); if (_keyboardshownUp == 1) { //Keyboard was already shown } else { [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.5]; //This will move the frame back to place _movableView.frame = CGRectMake(_movableView.frame.origin.x, _movableView.frame.origin.y+_variableMove, _movableView.frame.size.width, _movableView.frame.size.height); [UIView commitAnimations]; NSLog(@"Move View up"); _keyboardshownUp = 1; } } -(void) keyboardWillHide:(NSNotification *)notification { //The keyboard was shown, move movableView objects down NSLog(@"value of _keyboardshownUp is %i",_keyboardshownUp); if (_keyboardshownUp == 0) { //Keyboard was already hiden } else { [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.5]; //This will move the frame back to place _movableView.frame = CGRectMake(_movableView.frame.origin.x, _movableView.frame.origin.y-_variableMove, _movableView.frame.size.width, _movableView.frame.size.height); [UIView commitAnimations]; NSLog(@"Move View Down"); _keyboardshownUp = 0; } } UIKeyboardWillHideNotification一起执行此操作。如果我禁用UIKeyboardDidShowNotification,那么没有问题。但是,当我为“问题”textField:shouldChangeCharactersInRange:replacementString分配一个值时,好像键盘被隐藏(即使它不是)并且主UITextField恢复到其原始位置,因此键盘模糊不清风景。请帮助!!!

2 个答案:

答案 0 :(得分:2)

最近我面临这样的问题,这就是为什么我要创建一个库来面对这种情况。看看KeyboardAnimator。希望它符合您的要求。

答案 1 :(得分:0)

要移动UI控制器,在为UITextField分配值时,请使用“IQKeybordManager”库来解决此问题。 IQKeybordManager遇到了iPhone键盘向上滑动并覆盖UITextField / UITextView的问题。 IQKeyboardManager允许您防止键盘滑动问题并覆盖UITextField / UITextView,而无需您输入任何代码,也无需其他设置。要使用IQKeyboardManager,只需将源文件添加到项目中即可。