开始编辑时,Textfield在键盘后消失

时间:2013-02-19 08:50:01

标签: ios xamarin.ios

我在scrollview中有一个文本字段。 当我点击文本字段添加文本时,键盘弹出,文本字段消失在键盘后面。怎么解决这个? 滚动视图应向下滚动,以便在输入时文本字段仍然可见。

我为Objective-C项目找到了几个解决方案。不幸的是,我使用的是Mono Touch / C#。

我已经为textfield创建了一个委托。 我应该在方法“public override void 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)
    }
}

2 个答案:

答案 0 :(得分:1)

作为一个例子,这就是用ObjC解决它的方法。在这里,我只是移动包含文本字段的视图,使其可见(也许您可以将此代码翻译为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;
    }
}

答案 1 :(得分:0)

我在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);
        }
    }

我不认为这是最好的解决方案,但它运作正常。

相关问题