Bug 43937 - UIButton's Layer reference gets cleared when tapping.
Summary: UIButton's Layer reference gets cleared when tapping.
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 9.99 (iOS 10 previews)
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Future Cycle (TBD)
Assignee: Bugzilla
Depends on:
Reported: 2016-09-01 12:39 UTC by Tylerian
Modified: 2017-02-07 11:51 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

Demonstration project showing the error. (6.63 KB, application/zip)
2016-09-01 12:39 UTC, Tylerian

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Tylerian 2016-09-01 12:39:40 UTC
Created attachment 17293 [details]
Demonstration project showing the error.

Every time you tap an UIButton it's Layer gets re-referenced, therefore any custom state saved on it is gone.
Comment 1 Tylerian 2016-09-01 12:55:31 UTC
A good addition i've just found.

When you bind the layerClass native method to your UIButton and assign it to a CALayer subclass, the UIButton's Layer property doesn't get recreated anymore.

public class WeakButtonLayer : CALayer
    public WeakButtonLayer()
        Console.WriteLine("new WeakButtonLayer()");

public class WeakButton : UIButton
    public WeakReference<CALayer> WeakLayer;

    public static Class GetLayerClass()
        return new Class(typeof(WeakButtonLayer));

    public WeakButton(CGRect frame) : base(frame)
        this.WeakLayer = new WeakReference<CALayer>(this.Layer);
Comment 2 Alex Soto [MSFT] 2016-09-02 15:30:57 UTC

I could not reproduce this using an iPhone SE on 9.3.5 nor on an iPhone 6 Plus on 10.0.

Can you share with us your version information?

The easiest way to get exact version information is to use the 
"Xamarin Studio" menu, "About Xamarin Studio" item, "Show Details" 
button and copy/paste the version informations (you can use the 
"Copy Information" button).
Comment 3 Tylerian 2016-09-02 15:35:14 UTC
Below is my current installation info:

=== Xamarin Studio Community ===

Version 6.1 (build 5383)
Installation UUID: 4cdc71f8-5b1e-44d7-b378-b8c7087320cf
	Mono 4.6.0 (mono-4.6.0-branch/3ed2bba) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 406000182

=== NuGet ===


=== Xamarin.Profiler ===

Not Installed

=== Apple Developer Tools ===

Xcode 8.0 (11239.2)
Build 8S201h

=== Xamarin.iOS ===

Version: (Xamarin Studio Community)
Hash: 974ea0b
Branch: cycle8
Build date: 2016-08-30 18:12:06-0400

=== Build Information ===

Release ID: 601005383
Git revision: 37159a7d0c1ed6c9f661210879e9e233ca92e65d
Build date: 2016-08-30 22:07:20-04
Xamarin addins: 00a35f60101b50c8fa28b2b8c0e5b8ade85f7083
Build lane: monodevelop-lion-cycle8

=== Operating System ===

Mac OS X 10.11.6
Darwin El-Capitan.local 15.6.0 Darwin Kernel Version 15.6.0
    Thu Jun 23 18:25:34 PDT 2016
    root:xnu-3248.60.10~1/RELEASE_X86_64 x86_64

=== Enabled user installed addins ===

NuGet Package Management Extensions 0.11.1
Comment 4 Tylerian 2016-09-02 15:41:55 UTC
When you run the attached project, press the "Click me!" button.

The current (bugged) output for the attached project should be:
2016-09-02 17:36:38.188 ProofLeak[21859:1483917] WeakLayer.Target = 
2016-09-02 17:36:38.378 ProofLeak[21859:1483917] WeakLayer.Target = 
2016-09-02 17:36:38.690 ProofLeak[21859:1483917] WeakLayer.Target = 
2016-09-02 17:36:38.866 ProofLeak[21859:1483917] WeakLayer.Target = 

And the expected output for the attached project (upon fix) should be:
2016-09-02 17:37:55.494 ProofLeak[21871:1484949] WeakLayer.Target = <CALayer: 0x7b7f7700>
2016-09-02 17:37:55.643 ProofLeak[21871:1484949] WeakLayer.Target = <CALayer: 0x7b7f7700>
2016-09-02 17:37:55.796 ProofLeak[21871:1484949] WeakLayer.Target = <CALayer: 0x7b7f7700>
2016-09-02 17:37:55.939 ProofLeak[21871:1484949] WeakLayer.Target = <CALayer: 0x7b7f7700>
2016-09-02 17:37:56.091 ProofLeak[21871:1484949] WeakLayer.Target = <CALayer: 0x7b7f7700>

Best regards.
Comment 5 Alex Soto [MSFT] 2016-09-02 16:33:33 UTC
Hello Jairo

Yup that is the output that I am getting


Could you tell me on which device are you testing this?

Comment 6 Tylerian 2016-09-02 20:28:52 UTC
I've tested on every single device from iOS Simulator.
Also tested it on my physical device (iPhone 5) with no luck.

I'm Installing a fresh Xamarin copy on another Mac and going to testing it.

I'll keep you informed!
Comment 7 Timothy Risi 2016-12-08 01:33:09 UTC
Hi Jairo, did you ever get this tested on a fresh mac?
Comment 8 Manuel de la Peña [MSFT] 2017-01-03 09:52:49 UTC
@Jaro any comments or information regarding the issue?
Comment 9 Manuel de la Peña [MSFT] 2017-02-07 11:51:22 UTC
@Jaro If you are still experiencing this issue please re-open the bug report. Thanks!