Created
June 25, 2018 01:57
-
-
Save JoeM-RP/10e3900db27ea7f630f77e345fd02738 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using CoreGraphics; | |
using samples.core.Controls; | |
using samples.iOS.Effects; | |
using UIKit; | |
using Xamarin.Forms; | |
using Xamarin.Forms.Platform.iOS; | |
[assembly: ExportEffect(typeof(EntryMoveNextEffect), nameof(EntryMoveNextEffect))] | |
namespace samples.iOS.Effects | |
{ | |
public class EntryMoveNextEffect : PlatformEffect | |
{ | |
protected override void OnAttached() | |
{ | |
var entry = (UITextField)Control; | |
// Change return key to Don key | |
entry.ReturnKeyType = UIReturnKeyType.Done; | |
// Add the "done" button to the toolbar easily dismiss the keyboard | |
// when it may not have a return key | |
if (entry.KeyboardType == UIKeyboardType.DecimalPad || | |
entry.KeyboardType == UIKeyboardType.NumberPad) | |
{ | |
entry.InputAccessoryView = BuildDismiss(); | |
} | |
// Check if the attached element is of the expected type and has the NextEntry | |
// property set. if so, configure the keyboard to indicate there is another entry | |
// in the form and the dismiss action to focus on the next entry | |
if (base.Element is EntryMoveNextControl xfControl && xfControl.NextEntry != null) | |
{ | |
entry.ReturnKeyType = UIReturnKeyType.Next; | |
} | |
} | |
protected override void OnDetached() | |
{ | |
// Intentionally empty | |
} | |
private UIToolbar BuildDismiss() | |
{ | |
var toolbar = new UIToolbar(new CGRect(0.0f, 0.0f, Control.Frame.Size.Width, 44.0f)); | |
// Set default state of buttonAction/appearance | |
UIBarButtonItem buttonAction = new UIBarButtonItem(UIBarButtonSystemItem.Done, delegate { Control.ResignFirstResponder(); }); | |
// If we have a next element, swap out the default state for "Next" | |
if (base.Element is EntryMoveNextControl xfControl && xfControl.NextEntry != null) | |
{ | |
buttonAction = new UIBarButtonItem("Next", UIBarButtonItemStyle.Plain, delegate | |
{ | |
xfControl.OnNext(); | |
}); | |
} | |
toolbar.Items = new[] | |
{ | |
new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace), | |
buttonAction, | |
}; | |
return toolbar; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment