Skip to content

Instantly share code, notes, and snippets.

@jgold6
Last active July 18, 2018 17:20
Show Gist options
  • Save jgold6/5e098a0a940129948f2b to your computer and use it in GitHub Desktop.
Save jgold6/5e098a0a940129948f2b to your computer and use it in GitHub Desktop.
Date Picker Custom Renderer replacing Control
using System;
using Xamarin.Forms.Platform.iOS;
using Xamarin.Forms;
using TestiOSCutCopyPasteDisable.iOS;
using UIKit;
using Foundation;
using CoreGraphics;
using System.Security.AccessControl;
[assembly: ExportRenderer (typeof(DatePicker), typeof(DatePickerCustomRenderer))]
namespace TestiOSCutCopyPasteDisable.iOS
{
public class DatePickerCustomRenderer : DatePickerRenderer
{
CustomTextField newTextField;
public DatePickerCustomRenderer ()
{
}
protected override void OnElementChanged (ElementChangedEventArgs<DatePicker> e)
{
base.OnElementChanged (e);
if (Control != null) {
// do whatever you want to the UITextField here!
newTextField = new CustomTextField (Control.Frame);
newTextField.Text = Control.Text;
newTextField.Delegate = Control.Delegate;
newTextField.AddConstraints (Control.Constraints);
newTextField.InputView = Control.InputView;
var toolbar = (UIToolbar)Control.InputAccessoryView;
toolbar.Items [1].Clicked += (object sender, EventArgs evt) => {
newTextField.ResignFirstResponder ();
};
newTextField.InputAccessoryView = Control.InputAccessoryView;
SetNativeControl (newTextField);
// Control.ShouldChangeCharacters = ShouldChangeCharacters;
}
}
// public bool ShouldChangeCharacters(UITextField textField, NSRange range, string replacementString)
// {
// return false;
// }
}
public class CustomTextField : UITextField
{
public CustomTextField(CGRect frame) : base(frame)
{
}
public override bool CanPerform (ObjCRuntime.Selector action, NSObject withSender)
{
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment