我有一个容器,加载了一个UITableViewController
。一开始表格里没有数据,因此我展示一个空视图。这是我在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));
问题出在我禁用自动调整大小掩码的代码行。在iOS 7.1(设备和模拟器上)会出现以下错误:
NSInternalInconsistencyException Reason: Auto Layout still required after executing -layoutSubviews. UITableView's implementation of -layoutSubviews needs to call super.
如果我注释掉 TranslatesAutoresizingMaskIntoConstraints,一切都正常工作。相同的代码在iOS 8上运行时不需要修改。我仅仅移除了我的视图上的自动调整大小掩码而不是整个UITableView上的掩码。将该视图添加到表头会导致相同的问题。
如何解决自动布局的问题?虽然代码是用C#编写的,但您可以从Objective-C世界中提供解决方案,因为概念是相同的。