简体   繁体   English

开始编辑时,文本框消失在键盘后面

[英]Textfield disappears behind keyboard when start editing

I have a textfield within a scrollview. 我在scrollview中有一个文本字段。 When I tap into the textfield to add text, the keyboard pops up and the textfield disappears behind the keyboard. 当我点击文本字段以添加文本时,键盘弹出,并且文本字段消失在键盘后面。 How to solve this? 如何解决呢? The scrollview should scroll down so the textfield is still visible while typing. 滚动视图应向下滚动,以便在键入时仍可看到文本字段。

I found several solutions for an Objective-C project. 我为Objective-C项目找到了几种解决方案。 Unfortunately, I am using Mono Touch/C#. 不幸的是,我正在使用Mono Touch / C#。

I already created a delegate for the textfield. 我已经为文本字段创建了一个委托。 What should I add to the method "public override void EditingStarted (UITextField textField)" to make this work? 我应该在方法“公共重写无效EditingStarted(UITextField textField)”中添加什么以使其起作用?

public class CloseTextfieldDelegate : UITextFieldDelegate{
    private NewReportScreen controller;

    public CloseTextfieldDelegate(NewReportScreen newReportScreen)
    {
        controller = newReportScreen;
    }

    public override bool ShouldReturn (UITextField textField)
    {
        textField.ResignFirstResponder();
        return false;
    }

    public override void EditingStarted (UITextField textField)
    {
        //DO SOMETHING (MAKE TEXTFIELD VISIBLE SO IT DOESN'T DISAPPEARS BEHIND THE KEYBOARD)
    }
}

As an example, this is how it is solved with ObjC. 例如,这就是用ObjC解决的方法。 Here I just move the View that contains the textfield so it is visible (maybe you can translate this code to mono/C#. 在这里,我只是移动了包含文本字段的View,以便它可见(也许您可以将此代码转换为mono / C#。

- (void)textFieldDidBeginEditing:(UITextField *)textField
{

    if (textField == myTf)
    {
        CGRect rect = inputFieldsView.frame;
        rect.origin.y = -100;//move the view that contains the TextFiled up
        inputFieldsView.frame = rect;
    }
}

I solved the problem with the following method in the CloseTextfieldDelegate class: 我在CloseTextfieldDelegate类中使用以下方法解决了该问题:

public override void EditingStarted (UITextField textField)  //used to scroll the scrollview when editing a textfield
    {
        var yPositionTextFieldDescription =  (newReportController.usedTextFieldDescription.Frame.Location.Y - 143);
        var yPositionTextFieldRoom =  (newReportController.usedTextFieldRoom.Frame.Location.Y - 143);   

        if (textField == newReportController.usedTextFieldDescription){ 
            newReportController.usedScrollView.SetContentOffset (new PointF (0, yPositionTextFieldDescription), true);
        }
        else if (textField == newReportController.usedTextFieldRoom){
            newReportController.usedScrollView.SetContentOffset (new PointF (0, yPositionTextFieldRoom), true);
        }
    }

I don't think it is the best solution, but it works fine. 我认为这不是最好的解决方案,但是效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM