在iOS 7上向UITableView添加子视图时出现“仍需要自动布局”错误

3

我有一个容器,加载了一个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世界中提供解决方案,因为概念是相同的。
1个回答

0

我已经放弃了自动布局,不再在我的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;

现在它可以工作了,但我不知道是否打算不使用自动布局。也许有人能提供更好的答案,我会将其标记为已接受。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接