ios - 仅在 iOS 7 上向 UITableView 添加 subview 时出现 "Auto Layout still required"错误

标签 ios uitableview xamarin.ios autolayout autoresizingmask

我有一个加载 UITableViewController 的容器。一开始表中没有数据,所以我显示一个空 View 。这就是我在 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 上运行。我只删除了 View 上的自动调整大小掩码,而不是整个 UITableView。将 View 添加到表头会导致同样的问题。

我应该如何解决自动布局的问题?尽管代码在 C# 中,但您始终可以提供来自 Objective C 世界的解决方案,因为概念是相同的。

最佳答案

我不再使用自动布局,我的 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;

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

关于ios - 仅在 iOS 7 上向 UITableView 添加 subview 时出现 "Auto Layout still required"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26460934/

相关文章:

Zebra SDK 与 Monotouch 的绑定(bind)?

iOS 在 ImageView 中交叉淡入淡出多个图像

ios - 如何在 UINavigationBar 下方添加 UISegmentControl?

ios - UItableViewCell 大小不合适

ios - "*** Terminating app due to uncaught exception ' NSRangeException ', reason: ' *** -[__NSArrayM objectAtIndex :]: index 1 beyond bounds [0 . 。 0] '"

ios - 如何将 "method_getImplementation"和 "method_setImplementation"移植到 MonoTouch?

ios - UINavigationController 和 Swift 2.2 选择器中的自定义后退按钮

ios - Memcpy仅在iPhone 5s上崩溃

ios - 在 webRTC iOS 中设置视频大小

visual-studio - Xamarin Visual Studio 无法在联网 MAC 上启动构建服务器激活