首页 \ 问答 \ 如何阻止iOS键盘干扰UITextField位置(How to stop iOS keyboard interfering with UITextField position)

如何阻止iOS键盘干扰UITextField位置(How to stop iOS keyboard interfering with UITextField position)

我有一个键盘解雇的问题,我认为干扰了我的动画代码。 我想要发生的是,如果用户在键盘启动时单击搜索按钮,键盘将会下降,文本字段将显示在屏幕顶部。 就像在这张图片中一样

http://imgur.com/5dQ6aPZ

但是,如果在键盘启动时按下搜索按钮,实际发生的情况是这样的。

http://imgur.com/tp6Ffne

以下是“ 搜索”按钮的代码

- (IBAction)searchButton:(id)sender {

    [self.view endEditing:YES];
    [UIView transitionWithView:_enterRoomLabel
                        duration:0.4
                        options:UIViewAnimationOptionTransitionCrossDissolve
                        animations:NULL
                        completion:NULL];

    _enterRoomLabel.hidden = YES;
    [_srcButton setHidden:YES];
    [_srcAgainButton setHidden:NO];


    [UITextField beginAnimations:nil context:NULL];
    [UITextField setAnimationBeginsFromCurrentState:YES];
    [UITextField setAnimationDelegate:self];
    [UITextField setAnimationDuration:0.3];

    _roomCodeInput.frame = CGRectMake(_roomCodeInput.frame.origin.x - 0.0, (_roomCodeInput.frame.origin.y - 105.0), _roomCodeInput.frame.size.width + 0.0, _roomCodeInput.frame.size.height);
    [UITextField commitAnimations];


}

再次搜索按钮

- (IBAction)searchAgainButton:(id)sender {

[self.view endEditing:YES];
[UIView transitionWithView:_enterRoomLabel
                  duration:0.6
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:NULL
                completion:NULL];

_enterRoomLabel.hidden = NO;
[_srcButton setHidden:NO];
[_srcAgainButton setHidden:YES];


[UITextField beginAnimations:nil context:NULL];
[UITextField setAnimationBeginsFromCurrentState:YES];
[UITextField setAnimationDelegate:self];
[UITextField setAnimationDuration:0.3];

_roomCodeInput.frame = CGRectMake(_roomCodeInput.frame.origin.x + 0.0, (_roomCodeInput.frame.origin.y + 105.0), _roomCodeInput.frame.size.width - 0.0, _roomCodeInput.frame.size.height + 0.0);
[UITextField commitAnimations];
}

以下是我用来解除键盘的方法

UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];


- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[_roomCodeInput resignFirstResponder];
return NO;
}

-(void)dismissKeyboard{
[_roomCodeInput resignFirstResponder];
}

我只是想找到解决这个问题的最佳解决方案,或者重新设计类似的东西。 任何帮助将不胜感激! :)


I have a problem where the keyboard dismissal, I think interferes with the animation code I have. What I would like to happen is if the user clicks the search button while the keyboard is up, the keyboard drops and the text field will animate to the top of the screen. Like in this image.

http://imgur.com/5dQ6aPZ

However, what actually happens if the search button is pressed while the keyboard is up, is this.

http://imgur.com/tp6Ffne

Here is the code for the Search button:

- (IBAction)searchButton:(id)sender {

    [self.view endEditing:YES];
    [UIView transitionWithView:_enterRoomLabel
                        duration:0.4
                        options:UIViewAnimationOptionTransitionCrossDissolve
                        animations:NULL
                        completion:NULL];

    _enterRoomLabel.hidden = YES;
    [_srcButton setHidden:YES];
    [_srcAgainButton setHidden:NO];


    [UITextField beginAnimations:nil context:NULL];
    [UITextField setAnimationBeginsFromCurrentState:YES];
    [UITextField setAnimationDelegate:self];
    [UITextField setAnimationDuration:0.3];

    _roomCodeInput.frame = CGRectMake(_roomCodeInput.frame.origin.x - 0.0, (_roomCodeInput.frame.origin.y - 105.0), _roomCodeInput.frame.size.width + 0.0, _roomCodeInput.frame.size.height);
    [UITextField commitAnimations];


}

Search Again button

- (IBAction)searchAgainButton:(id)sender {

[self.view endEditing:YES];
[UIView transitionWithView:_enterRoomLabel
                  duration:0.6
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:NULL
                completion:NULL];

_enterRoomLabel.hidden = NO;
[_srcButton setHidden:NO];
[_srcAgainButton setHidden:YES];


[UITextField beginAnimations:nil context:NULL];
[UITextField setAnimationBeginsFromCurrentState:YES];
[UITextField setAnimationDelegate:self];
[UITextField setAnimationDuration:0.3];

_roomCodeInput.frame = CGRectMake(_roomCodeInput.frame.origin.x + 0.0, (_roomCodeInput.frame.origin.y + 105.0), _roomCodeInput.frame.size.width - 0.0, _roomCodeInput.frame.size.height + 0.0);
[UITextField commitAnimations];
}

And here are the methods I'm using to dismiss the keyboard

UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];


- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[_roomCodeInput resignFirstResponder];
return NO;
}

-(void)dismissKeyboard{
[_roomCodeInput resignFirstResponder];
}

I'm just trying to find the best solution to solve this problem or perhaps re-design to something similar. Any Help would be greatly appreciated! :)


原文:https://stackoverflow.com/questions/27801482
更新时间:2019-12-13 08:09

最满意答案

替换您的代码如下:希望它现在可以工作

搜索按钮:

- (IBAction)searchButton:(id)sender {

    //[UITextField commitAnimations];
    [UIView animateWithDuration:0.5 animations:^{
        _enterRoomLabel.hidden = YES;
        [_srcButton setHidden:YES];
        [_srcAgainButton setHidden:NO];

        _roomCodeInput.transform = CGAffineTransformMakeTranslation(0.0, -100.0);
    } completion:^(BOOL finished) {
        // when animation
        [UIView animateWithDuration:0.5 animations:^{
        }];
    }];



}

再次搜索按钮

- (IBAction)searchAgainButton:(id)sender {

    [UIView animateWithDuration:0.5 animations:^{
        _enterRoomLabel.hidden = NO;
        [_srcAgainButton setHidden:YES];
        _roomCodeInput.text=Nil;

        _roomCodeInput.transform = CGAffineTransformMakeTranslation(0.0, 0.0);
    } completion:^(BOOL finished) {
        // when animation
        [UIView animateWithDuration:0.5 animations:^{
            [_srcButton setHidden:NO];

        }];
    }];

Replace your code as follows: Hope it will work now

Search button:

- (IBAction)searchButton:(id)sender {

    //[UITextField commitAnimations];
    [UIView animateWithDuration:0.5 animations:^{
        _enterRoomLabel.hidden = YES;
        [_srcButton setHidden:YES];
        [_srcAgainButton setHidden:NO];

        _roomCodeInput.transform = CGAffineTransformMakeTranslation(0.0, -100.0);
    } completion:^(BOOL finished) {
        // when animation
        [UIView animateWithDuration:0.5 animations:^{
        }];
    }];



}

Search Again button

- (IBAction)searchAgainButton:(id)sender {

    [UIView animateWithDuration:0.5 animations:^{
        _enterRoomLabel.hidden = NO;
        [_srcAgainButton setHidden:YES];
        _roomCodeInput.text=Nil;

        _roomCodeInput.transform = CGAffineTransformMakeTranslation(0.0, 0.0);
    } completion:^(BOOL finished) {
        // when animation
        [UIView animateWithDuration:0.5 animations:^{
            [_srcButton setHidden:NO];

        }];
    }];
2015-01-06

相关问答

更多

jsPlumb draggable element javascript函数(jsPlumb draggable element javascript function)

你错过了jsPlumb.setContainer($("body")); https://jsfiddle.net/mkaran/mLthybzo/ https://jsplumbtoolkit.com/community/doc/home 编辑:你的例子的小提琴https://jsfiddle.net/mkaran/aof6mq34/ You are missing the jsPlumb.setContainer($("body")); https://jsfiddle.net/mkaran

如何在同一帐户上设置“Dev repo”(在prod和团队之间)(How to set up a “Dev repo” (between the prod and the team) on the same account)

在git中似乎存在一个基本的误解。 您有一个通过设置组织正确找到的回购。 那很棒。 你和你的开发人员在他们的回购中单独分工。 那也没关系。 现在,如果您是首席开发人员,并且想要了解人们的工作方式,那么您可以在GitHub上的中央组织中组织所有这些。 即开发者发送拉动请求并将它们拉入开发分支,您不必将它们拉入主控。 在您的开发分支中,您可以测试合并的代码,看它是否符合您的需求。 如果您不喜欢它,则不必将其与生产和/或主分支合并。 在git中,你使用分支机构来理清好事和坏事。 你批准的所有东西都合并

Proguard之后setBackgroundColor不起作用(setBackgroundColor does not work after Proguard)

Proguard通过重命名类和方法来缩短代码,以缩短名称并删除未引用的代码。 您的代码不起作用,因为Proguard重命名或删除了ColorId方法black() , white()和red() 。 要使用反射,您需要添加Proguard keep指令来告诉它保留这些方法并保留其原始名称。 我没有解释为什么“后”代码的switch语句搞砸了。 你确定你正确反编译了吗? 为什么“之前”代码如此错综复杂? 它使用反射按颜色名称查找方法,然后调用它将颜色名称转换为String,解析String以获取整

运行SQLite的进程如何知道某个特定页面已被另一个进程更新?(How a process running SQLite knows that a particular page has been updated by another process?)

SQLite在事务完成时写入所有已更改的页面; 一旦允许另一个连接读取,就没有脏页面。 要检测其他连接所做的更改 ,数据库头中有文件更改计数器 。 但是,它不适用于特定页面,而是适用于整个数据库。 SQLite writes all changed pages when a transaction finishes; once another connection is allowed to read, there are no dirty pages. To detect changes ma

使用Java获取HTML - 某些字符未正确获取(fetch HTML with Java - some characters not fetched correctly)

DataInputStream readLine()已过时。 您应该使用Reader,它可以正确处理从字节到字符的转换。 如果使用InputStreamReader ,则可以在构造函数中指定编码,如果将其包装在BufferedReader ,则可以读取行。 代替 DataInputStream dis = new DataInputStream(new BufferedInputStream(is)); 你可以有 BufferedReader reader = new BufferedRe

获得第一个ul和第一个li的第一个输入(Get First Input in first ul and its first li)

ul不是h3的孩子。 这是一个兄弟姐妹。 调整XPath以使用following-sibling:: axis //h3[text()='{0}']/following-sibling::ul/li/input[1] 如果要确保选择第一个ul和第一个li ,请添加其他谓词过滤器: //h3[text()='{0}']/following-sibling::ul[1]/li[1]/input[1] The ul is not a child of h3. It is a sibling. Ad

错误:尝试返回arraylist时不兼容的类型(Error: Incompatible types when attempting to return an arraylist)

罪魁祸首是这样的: for (Long m : divisors) { return m; } 在此,您返回Long值,而方法签名指定返回ArrayList public static **ArrayList<Long>** check(long num) 您需要更改方法签名或根据要完成的内容返回适当的值。 The culprit is this : for (Long m : divisors) { return m; } In this you a

查找连续两周的第一个日期,其中记录为R(data.table)(Find first date of two consecutive weeks with records in R (data.table))

我认为这有效: # set the key for the rolling merges setkey(dt, group, date) # find start and end point of the intervals you want start = dt[J(group, date + 7 ), .I, roll = -Inf, by = .EACHI]$I end = dt[J(group, date + 13), .I, roll = Inf, by = .EACHI]$I

相关文章

更多

最新问答

更多
  • jsPlumb draggable element javascript函数(jsPlumb draggable element javascript function)
  • MVC4:ViewModel(带有radiobuttonlist)在HttpPost之后为空(MVC4: ViewModel (with radiobuttonlist) is empty after HttpPost)
  • 如何在同一帐户上设置“Dev repo”(在prod和团队之间)(How to set up a “Dev repo” (between the prod and the team) on the same account)
  • 如何在tcl中将eth0配置为发送方udp端口(how to configure eth0 as a sender udp port in tcl)
  • 在main方法中进行更改后传递给构造函数的TreeMap实例的行为是什么(What is the behavior of a TreeMap instance passed into a constructor following changes in main method)
  • 在这个的一些属性不同的颜色(Different color in some properties of this)
  • CURL没有返回任何内容(CURL Not returning anything)
  • MVC5注入依赖于城堡windsor的视图(MVC5 injecting dependency on a view with castle windsor)
  • CakePHP AJAX-Call:发生错误:未定义(CakePHP AJAX-Call: An error occured: undefined)
  • 如何用.aidl扩展名创建文件?(how to create a file with .aidl extension?)
  • 无法获得全尺寸动态WordPress灯箱图像显示(Cannot get full size dynamic WordPress lightbox image to display)
  • 使用Jsoup删除元素不起作用(Removing Element with Jsoup doesn't work)
  • 交叉编译ARM的MongoDB C ++驱动程序。(Cross compiling MongoDB C++ driver for ARM. Cannot find Boost Libraries)
  • ProgressDialog没有关闭(ProgressDialog not closing)
  • 单元测试DB中的JPA插入(Unit testing JPA insertion in the DB)
  • 点击谷歌服务对话框中的更新按钮(Click on update button in check google service dialog)
  • 适用于PhoneGap Build的iOS“缺少必需的57x57图标”(“Missing required 57x57 icon” for iOS on PhoneGap Build)
  • MVC ckeditor post编辑器值内联(MVC ckeditor post editor value inline)
  • Angular 2 Pipe - 无法读取未定义的属性'toString'(Angular 2 Pipe - Cannot read property 'toString' of undefined)
  • 安装引导加载程序(Installing bootloaders)
  • 用于确定最终如何包含头文件的工具(Tool to figure out how a header file is eventually being included)
  • 在将字符串转换为int [duplicate]之前检查字符串是否不是数字(Check if string is not a number before converting it to a int [duplicate])
  • 从脚本构建db2数据库以进行构建验证的工具?(Tools to build db2 database from scripts for build verification?)
  • 在偏移之后向FileOutputStream写一个数字(Write a number to FileOutputStream after an offset)
  • Javascript RegEx仅限数字(无特殊字符)(Javascript RegEx for Numbers Only (no special characters))
  • 我可以在PHP的Linux服务器上运行新的COM(“WScript.Shell”)(Can I run new COM(“WScript.Shell”) on linux servers in PHP)
  • 在Backbone.js中放置与视图不直接相关的代码(例如广告,分析等)的位置?(Where to place code not directly related to a view (such as ads, analytics etc) in Backbone.js?)
  • 适用于Mac和PC的一般开发人员必备软件综合列表[关闭](Comprehensive List of Essential Software for General Developers on Mac and PC [closed])
  • 将语言文本转换为模型(Transformation Language Text to Model)
  • 在控制器中的两个方法之间共享变量?(Sharing variables between two methods in a controller?)