[iOS] Check new element before creating placeholder label (#5432)
authorE.Z. Hart <hartez@users.noreply.github.com>
Tue, 5 Mar 2019 18:06:27 +0000 (11:06 -0700)
committerSamantha Houts <samhouts@users.noreply.github.com>
Tue, 5 Mar 2019 18:06:27 +0000 (10:06 -0800)
* Check new element before creating placeholder label

* additional checks

* create placeholder once

Xamarin.Forms.Platform.iOS/Renderers/EditorRenderer.cs

index 6bfdeca..e14d888 100644 (file)
@@ -31,15 +31,23 @@ namespace Xamarin.Forms.Platform.iOS
 
                protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
                {
-                       // create label so it can get updated during the initial setup loop
-                       _placeholderLabel = new UILabel
+                       bool initializing = false;
+                       if (e.NewElement != null && _placeholderLabel == null)
                        {
-                               BackgroundColor = UIColor.Clear
-                       };
+                               initializing = true;
+                               // create label so it can get updated during the initial setup loop
+                               _placeholderLabel = new UILabel
+                               {
+                                       BackgroundColor = UIColor.Clear
+                               };
+                       }
 
                        base.OnElementChanged(e);
 
-                       CreatePlaceholderLabel();
+                       if (e.NewElement != null && initializing)
+                       {
+                               CreatePlaceholderLabel();
+                       }
                }
 
                protected internal override void UpdateFont()
@@ -63,6 +71,9 @@ namespace Xamarin.Forms.Platform.iOS
 
                void CreatePlaceholderLabel()
                {
+                       if (Control == null)
+                               return;
+
                        Control.AddSubview(_placeholderLabel);
 
                        var edgeInsets = TextView.TextContainerInset;