画面の下部にtextFieldがあります。編集を開始すると、キーボードが表示され、textFieldが非表示になります。
誰かがこれを解決する方法を提案できますか?
画面の下部にtextFieldがあります。編集を開始すると、キーボードが表示され、textFieldが非表示になります。
誰かがこれを解決する方法を提案できますか?
通常、コンテンツをスクロールビューに配置し、キーボードが表示されたらスクロールビューを上に移動します。そこに多くのチュートリアルがあるので、それらをグーグルで検索してください。Here
それらの1つです。
次のコードを2つの異なる方法で使用し、編集の開始時と終了時に呼び出して、ビューを上下に移動できます(yourView.frame.origin.y-100
必要に応じて調整するために減算される値wを変更するだけです)
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.5];
yourView.frame = CGRectMake(yourView.frame.origin.x, yourView.frame.origin.y-100, yourView.frame.size.width, yourView.frame.size.height);
[UIView commitAnimations];
このコードスニペットを参照してください。そして、1つのUIButtonを使用してUIKeyboardを閉じます。
- (void) animateTextField: (UITextField *)textField up:(BOOL) up
{
const int movementDistance = 200;
const float movementDuration = 0.4f;
int movement = (up ? -movementDistance : movementDistance);
up ? (self.button.enabled= YES) : (self.button.enabled = NO);
[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
-(void)textFieldDidBeginEditing:(UITextField*) inTextField
{
[self animateTextField:mText up:YES];
[self addObservers];
}
- (void)addObservers
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:@"UITextFieldTextDidChangeNotification" object:nil];
}
- (void)textFieldDidChange:(NSNotification*)aNotification
{
NSLog(@"TextField data=%@",mText.text);
}
- (IBAction) doneBtn:(id)sender
{
[mText resignFirstResponder ];
[self animateTextField:mText up:NO];
}
textFieldキーボードを上にタップすると、textFieldが非表示になります。したがって、必要に応じて、keyboardresignではなくビューを作成する必要があります。だからあなたが使用を表示するために
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField delegate for the logic. now see the code
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textField.tag==1) //add tag to your textField to identify it
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.3];
CGRect viewFrame=self.view.frame;
viewFrame=sinViewFrame.size.height+100;
viewFrameorigin.y-=100; // as required
self.view.frame=viewFrame;
[UIView commitAnimations];
}
}
お役に立てば幸いです。
この問題を解決するために自動レイアウト制約を使用します。制約をプロパティとして保持し、キーボードが表示/非表示になったときにそれを操作します。テキストフィールドの上にあるビューを画面の外に移動し、テキストフィールドを上に移動するだけです。編集が終わったら、制約で元の位置に移動します。
-(void)keyboardWillShow:(NSNotification*)notification{
[UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionTransitionNone animations:^{
self.topSpaceLayoutConstraint.constant = -150;
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
} completion:NULL];
}
-(void)keyboardWillHide:(NSNotification*)notification
{
[UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionTransitionNone animations:^{
self.topSpaceLayoutConstraint.constant = 10;
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
} completion:NULL];
}
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpaceLayoutConstraint;