Reputation: 721
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)
}
}
Upvotes: 1
Views: 1099
Reputation: 721
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.
Upvotes: 0
Reputation: 21137
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;
}
}
Upvotes: 1