Search code examples
iosuitableviewxamarin.iosautolayoutautoresizingmask

"Auto Layout still required" error when adding subview to UITableView only on iOS 7


I have a container which loads an UITableViewController. On the beginning there is no data in the table so I show an empty view. This is how I do it in ViewDidLoad:

emptyView = new UIView ();
emptyView.BackgroundColor = UIColor.Gray;
// works on iOS 8 but has to be removed on iOS 7
emptyView.TranslatesAutoresizingMaskIntoConstraints = false;

var views = NSDictionary.FromObjectsAndKeys(
    new object[] { emptyView },
    new object[] { "emptyView" }
);

View.AddSubview (emptyView);
this.TableView.SeparatorStyle = UITableViewCellSeparatorStyle.None;

View.AddConstraints(NSLayoutConstraint.FromVisualFormat ("V:|-0-[emptyView]-0-|", NSLayoutFormatOptions.DirectionLeadingToTrailing, null, views));
View.AddConstraints(NSLayoutConstraint.FromVisualFormat ("H:|-0-[emptyView]-0-|", NSLayoutFormatOptions.DirectionLeadingToTrailing, null, views));

The problem is now the line where I disable the autoresizing mask. On iOS 7.1 (on device and simulator) I get the following error:

NSInternalInconsistencyException Reason: Auto Layout still required after executing -layoutSubviews. UITableView's implementation of -layoutSubviews needs to call super.

If I comment out TranslatesAutoresizingMaskIntoConstraints everything works fine. The same code is running without modifications on iOS 8. I only remove the autoresizing mask on my view and not on the whole UITableView. Adding the view to the table header leads to the same problem.

How should I solve the problem with Auto Layout? Although the code is in C# you can always provide solutions from the Objective C world because the concepts are the same.


Solution

  • I moved away from auto layout and I'm not using it anymore for my emptyView.

    emptyView = new UIView (new RectangleF(0, 0, this.View.Bounds.Size.Width, this.View.Bounds.Size.Height));
    emptyView.BackgroundColor = UIColor.White;
    emptyView.AutoresizingMask = UIViewAutoresizing.All;
    

    Now it works, but I don't know if it is intended to not use auto layout. Perhaps one could provide a better answer which I could mark as accepted.