键盘隐藏 UITextField。怎么解决这个问题呢?

发布于 2024-11-17 02:19:46 字数 66 浏览 2 评论 0原文

我有一个位于屏幕底部的文本字段。当我开始编辑时,键盘会出现并隐藏文本字段。

谁能建议如何解决这个问题?

I have a textField which is at bottom of the screen. When I begin editing, the keyboard appears and hides the textField.

Can anyone suggest how to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

给我一枪 2024-11-24 02:19:46

通常,您会将内容放在滚动视图中,并在键盘出现时向上移动滚动视图。那里有很多教程,所以谷歌一下。 此处 就是其中之一。

Generally you would put your content in a scroll view and move your scroll view up when the keyboard appears. There are many tutorials out there so google them. Here is one of them.

清秋悲枫 2024-11-24 02:19:46

您可以在两个不同的方法中使用以下代码,在编辑开始和结束时调用它,上下移动视图,(只需更改 yourView.frame.origin.y-100 中减去的值即可根据您的需要进行调整)

    [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];

You can use following code in two diffrent method, call it when editing began and end, to move your view up and down,(just change the valuew that is been subtracted in yourView.frame.origin.y-100 to adjust according to your need)

    [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];
不念旧人 2024-11-24 02:19:46

请参阅此代码片段。并使用一个 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];
}

See this code snippet. And use one UIButton to dismiss 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];
}
孤芳又自赏 2024-11-24 02:19:46

当您点击文本字段键盘向上时,您的文本字段将隐藏在其旁边。所以你需要什么,你需要让你的视图不键盘重新签名是一个解决方案。所以为了让您查看使用

- (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];

   }

}

希望它对您有帮助。

When you tap on textField keyboard up then your textField hide by it. so waht you need, you need to make your view up not keyboardresign is a solution. so for making you view up use

- (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];

   }

}

hope it helps you.

江湖正好 2024-11-24 02:19:46

我使用自动布局约束来解决这个问题。我将约束作为一种属性,并在键盘出现/消失时使用它。我只需将文本字段上方的视图移出屏幕,然后将文本字段移至顶部。完成编辑后,我通过约束将它们移动到原始位置。

-(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;

I use auto layout constraint to solve this problem. I hold constraint as a property, and play with it when keyboard appears / disappears. I simply move views above the textfield out of screen, and textfield to the top. After finishing edit, I move them by the constraint to their original position.

-(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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文