2015-03-25 4 views
0

Я просто хочу, чтобы UITextField поднимался, когда была представлена ​​клавиатура. Я достиг этого, установив рамку UITextField после того, как была представлена ​​клавиатура. Он работает для обычных случаев. Но, если пользователь включил предсказание в клавиатуре по умолчанию, тогда интеллектуальная текстовая область помещается над UITextField. В этом случае я хочу, чтобы текстовое поле двигалось вверх. Я попытался установить inputAccessoryView текстового поля. Но приложение разбилось без отчета. Так как я могу установить inputAccessoryView текстового поля для достижения моего требования.Настройка inputAccessoryView для UitextField

textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, bottomToolbarView.frame.size.width, bottomToolbarView.frame.size.height)]; 
textField.delegate = self; 
textField.textAlignment = NSTextAlignmentCenter; 
textField.inputAccessoryView = bottomToolbarView; 
[textField setReturnKeyType:UIReturnKeyGo]; 
[textField setPlaceholder:@"Enter Employee Id"]; 
[textField setBackgroundColor:[UIColor colorWithRed:1.000 green:0.720 blue:0.611 alpha:1.000]]; 
[bottomToolbarView addSubview:textField]; 

Simulator Image

ответ

0

попробуйте этот код в текстовое поле ли начать редактирование метод делегата

CGRect textFieldRect = 
[self.view.window convertRect:textField.bounds fromView:textField]; 
CGRect viewRect = 
[self.view.window convertRect:self.view.bounds fromView:self.view]; 

CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height; 

CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size. height; 

CGFloat denominator = 
(MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height; 

CGFloat heightFraction = numerator/denominator; 

if (heightFraction < 0.0) 
{ 
    heightFraction = 0.0; 
} 
else if (heightFraction > 1.0) 
{ 
    heightFraction = 1.0; 
} 

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 

if (orientation == UIInterfaceOrientationPortrait || 
    orientation == UIInterfaceOrientationPortraitUpsideDown) 
{ 
    animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction); 
} 
else 
{ 
    animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction); 
} 

CGRect viewFrame = self.view.frame; 
viewFrame.origin.y -= animatedDistance; 

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 

[self.view setFrame:viewFrame]; 

[UIView commitAnimations]; 

& по методу делегата didEndEditing

-(void)textFieldDidEndEditing:(UITextField *)textField1 
{ 
    CGRect viewFrame = self.view.frame; 
    viewFrame.origin.y += animatedDistance; 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 

    [self.view setFrame:viewFrame]; 
    [UIView commitAnimations]; 

} 
+0

Bro Я не хочу использовать этот метод изменения кадра UITextField каждый раз, когда отображается клавиатура или скрывается потому что высота клавиатуры не постоянна, и она отличается от Iphone 6 и Iphone 6 plus. Поэтому направляйте меня, чтобы достичь этого с помощью inputAccessoryView UITextField, потому что этот метод намного эффективнее и универсален, чем предыдущий. –

+0

Он будет работать для всех типов экранов устройств, таких как iphone 6,6+ – poojathorat

+0

inputAccessoryView предназначен для установки панели инструментов или для отображения Pickerview на текстовом поле – poojathorat

Смежные вопросы