UIView 通过 uitextfield 键盘自动移动 [英] UIView automatically moving by uitextfield keyboard

查看:25
本文介绍了UIView 通过 uitextfield 键盘自动移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我手头有一个非常奇怪的问题,数小时的搜索没有提供任何信息.

So i have a really weird problem at my hands and hours of search has provided nothing.

我有一个包含 uitextfield 的 uiview.现在最初这个视图在可见屏幕之外,坐标类似于 x=-500,y=-500.

I have a uiview containing a uitextfield. Now initially this view is outside of the visible screen with coordinates like x=-500,y=-500.

然而,当按下按钮时,这个视图会动画化并移动到屏幕中央.

However in response to a button press this view animates and moves into the center of the screen.

现在,每当我点击作为此视图的子视图的 uitextfield 时.此视图将移回屏幕外的原始坐标.

Now whenever i tap on a uitextfield that is the subview of this view.This view moves back to its original coordinates outside the screen.

我已经疯狂地检查了我的代码,一旦进入,没有任何东西再次将视图移到外面.如果能帮助解释这种非常陌生的行为,我们将不胜感激.

I have frantically checked my code and there is nothing that is moving the view outside again once its in. Any help in explaining this very unfamiliar behaviour would be really appreciated.

这段代码将视图移动到屏幕上

This code moves the view onto the screen

- (IBAction)Register:(id)sender {
//(self.view.frame.size.width/2)-(self.SignUp_Screen.frame.size.width/2);
//self.login_Screen.hidden = YES;
self.blurView.hidden = NO;
//self.SignUp_Screen.layer.zPosition = 5;
NSLog(@"Register");
self.SignUp_Screen.hidden = NO;

[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.SignUp_Screen.frame = CGRectMake(35, 50,self.SignUp_Screen.frame.size.width , self.SignUp_Screen.frame.size.height);
} completion:^(BOOL finished) {

}];
}

这些是文本字段的委托方法

and these are the delegate methods for the textfield

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    NSLog(@"TextFieldEndEditing");
    [textField resignFirstResponder];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"textFieldShouldReturn");
    [textField resignFirstResponder];
    return YES;
}

推荐答案

正如 Wezly 所暗示的,如果您正在使用自动布局,则不再直接修改框架.那是旧世界.您想要为约束设置一个 Outlet/属性并为其设置动画.

As Wezly hints at, if you are using autolayout, you don't modify the frame directly anymore. That's the old world. You want to have an Outlet / property for the constraint and animate it.

[UIView animateWithDuration:0.25
                 animations:^{
                   SignUp_Screen.centerXConstraint.constant = ...;
                   SignUp_Screen.centerYConstraint.constant = ...;
                   [SignUp_Screen layoutIfNeeded];
 }];

请参阅此处此处了解更多详情.

See here and here for more details.

这篇关于UIView 通过 uitextfield 键盘自动移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆