【问题标题】:Move View When Keyboard Appears - Only Upon Editing?键盘出现时移动视图 - 仅在编辑时?
【发布时间】:2012-11-15 18:34:27
【问题描述】:

我在这里使用了 Apple 在其文档中的演示代码:

http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html#//apple_ref/doc/uid/TP40009542-CH5-SW7

让我的视图根据正在编辑的 textField 相应移动。它工作得很好,我的观点按照我的预期移动,除了一件事。

我的问题是,我可以选择一个 textField,它只会在我开始输入时向上移动视图,而不是在我实际选择 textField 时。

我确实使用了与文档中相同的代码(按照上面的链接)。非常令人沮丧,我看不出是什么原因造成的。谢谢。

【问题讨论】:

  • 您应该会看到我的编辑。这将按您的需要工作。

标签: iphone objective-c uiview uikeyboard


【解决方案1】:

这是逻辑见。

1)您需要一个Flag 值集TRUE,最初位于ViewDidLoadviewWillAPpear。 假设isNeedToMove 是那个标志值。

您需要在您的代码中实现这些方法,使用它们时不要忘记在您的UIViewcontroller.h classAdopt 协议UITextFieldDelegate

编辑:在这里我已经更改了您在您的评论中提到的代码您需要在触摸 TextFIeld 时移动该 UIView。请参阅下面是一些代码的逻辑。

将目标添加到ViewDiodiLoad 中的TextField

- (void)viewDidload
{
 [touchyTextField addTarget:self
                    action:@selector(yourDesiredMethod)
          forControlEvents:UIControlEventTouchDown];
}

-(void)yourDesiredMethod
{
if(isNeedToMove)//this Flag Avoid The unnecessary move call.
{
 //here call the method which Move The UIview
 }
}


- (BOOL)textFieldShouldReturn:(UITextField*)textField
{

[textField resignFirstResponder];
//here call the method which move UIView to its actual position.
 isNeedToMove= TRUE;
 return YES;

}

希望你能有所了解。

【讨论】:

  • @Josh Kahane 你还有这个问题吗..?
  • 目前不在我的电脑上,稍后会尝试并报告!
  • 好的,我试过了,没有运气,同样的结果。视图仅在键入时向上移动,在输入文本字段时不会向上移动。
  • @JoshKahane 在说什么......?这是你的错误。我知道你说I can select a textField and it will only move the view up when I begin typing and not when I actually select the textField。我只是根据它给出答案
  • @JoshKahane 查看我的答案,我已对其进行了编辑,它会根据您的需要工作。
【解决方案2】:

我也遇到过这个问题。在keyboardWasShown 中,Apple 文档说:

 if (!CGRectContainsPoint(aRect, activeField.frame.origin) 

我用过:

if (activeField == myTextField)

这是答案的长版本:moving content located under the keyboard

【讨论】:

    猜你喜欢
    • 2010-12-19
    • 2018-05-21
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-25
    • 2023-03-08
    相关资源
    最近更新 更多