Bug 35325 - LayoutSubviews called before constructor in UITableViewHeaderFooterView subclass
Summary: LayoutSubviews called before constructor in UITableViewHeaderFooterView subclass
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 9.0 (iOS9)
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2015-10-28 06:32 UTC by BB
Modified: 2015-11-24 05:05 UTC (History)
7 users (show)

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

Sample & Logs (20.77 KB, application/zip)
2015-11-20 18:24 UTC, Kent Green [MSFT]

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 BB 2015-10-28 06:32:23 UTC
After upgrading to the latest Xamarin Studio (5.9.7), Xamarin.iOS (, there is a problem where I am overriding UITableViewHeaderFooterView:- 

	public class AlterationHeaderFooterView : UITableViewHeaderFooterView
		private readonly UILabel label;

		public AlterationHeaderFooterView() : base()
			this.label = new UILabel();

		public override void LayoutSubviews()
			this.label.Frame = new CGRect(15.0f, this.ContentView.Bounds.Height - 24.0f, this.ContentView.Bounds.Width, 24.0f);

The problem is that when I call:

    new AlterationHeaderFooterView()

LayoutSubviews is called BEFORE the code in the constructor runs and so "this.label" is null and crashes in LayoutSubviews

It looks like the call to base() constructor is doing something that means LayoutSubviews is called.

This didn't happen in the previous version I was using: Xamarin Studio 5.9.4, Xamarin.iOS
Comment 1 Kent Green [MSFT] 2015-11-20 18:24:35 UTC
Created attachment 13927 [details]
Sample & Logs

I was able to reproduce the issue based on the information provided in the description and the following desk case: https://kb.xamarin.com/agent/case/232094.

Based on this, I've created a sample project containing the code snippet, as well as the logs from my reproduction of the issue.
Comment 3 Rolf Bjarne Kvinge [MSFT] 2015-11-23 14:02:55 UTC
This is just how it's implemented by Apple in iOS 9 (in earlier versions of iOS it's different).

The same thing happens in Objective-C: https://gist.github.com/rolfbjarne/d5cc9208c1df8e5d2841
Comment 4 BB 2015-11-23 14:19:15 UTC
Hi Rolf, I don’t think your test code is quite testing the exact issue.

You need to have a constructor for AlterationHeaderFooterView and log a message inside there.

Then check that that constructor message comes before the layoutSubviews one.
Comment 5 Rolf Bjarne Kvinge [MSFT] 2015-11-24 03:17:56 UTC
I've updated the Objective-C test case to show that a log message from layoutSubviews occur before a log message in the constructor with iOS 9.1 (but not iOS 8.2).
Comment 6 BB 2015-11-24 04:50:50 UTC
Interesting and a bit worrying in case Apple have introduced the same problem in any other classes...

Has Xamarin reported the bug to Apple?
Comment 7 Rolf Bjarne Kvinge [MSFT] 2015-11-24 05:05:34 UTC
No, we haven't reported anything to Apple (feel free to use my sample code if you wish to do so though).