1

我喜欢 iOS 6 中新的自动布局功能,但是在将它与我的 MKAnnotationView 子类结合使用时遇到了一些麻烦。

我在初始化方法中禁用了自动调整掩码转换。

self.translatesAutoresizingMaskIntoConstraints = NO;

但是当我加载使用我的子类注释的 MKMapView 时,该应用程序会引发 NSInternalInconsistencyException。

*** Assertion failure in -[ENMapAnnotationView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2372/UIView.m:5776
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. ENMapAnnotationView's implementation of -layoutSubviews needs to call super.'

我没有覆盖 -layoutSubviews 所以对我来说这看起来苹果的 MKAnnotationView 实现还没有准备好进行自动布局。是否有一些聪明的方法可以解决 MKAnnotationView 中缺乏自动布局支持的问题,以便我可以在我的子类中使用自动布局?

4

3 回答 3

6

注释视图的自动布局在 iOS SDK 8 之前运行良好。在 iOS SDK 8 中,我看到具有动态约束的视图的行为非常奇怪,它们在地图上移动,有时甚至对齐左上角。

解决方案是setTranslatesAutoresizingMaskIntoConstraints:YES

[annotationView setBounds:CGRectMake(0, 0, width, height)];

并使用从子视图计算的宽度和高度。然后至少您可以对子视图使用约束。

于 2014-10-01T14:58:00.620 回答
4

不要禁用 ENMapAnnotationView 的 translatesAutoresizingMaskIntoConstraints(否则您会错过 Pin 图)。并确保启用要添加到 ENMapAnnotationView 的 translatesAutoresizingMaskIntoConstraints 的视图!!!

photoViewAddToAnnotationView.translatesAutoresizingMaskIntoConstraints = YES;

而已!我们不需要关闭自动布局(关闭自动布局也可以解决问题'

于 2013-09-23T19:12:49.000 回答
-1

可能您必须在 ENMapAnnotaitonView 类中实现 - (void)layoutSubviews 方法。

于 2013-01-29T20:59:55.863 回答