简体   繁体   中英

Textfield disappears behind keyboard when start editing

I have a textfield within a 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. Unfortunately, I am using 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?

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. Here I just move the View that contains the textfield so it is visible (maybe you can translate this code to 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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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