thi*_*sai 1 iphone mapkit mkannotationview ios autolayout
我喜欢iOS 6中的新自动布局功能,但是当我将它与MKAnnotationView子类结合使用时,我遇到了一些麻烦.
我在初始化方法中禁用了自动调整掩码转换.
self.translatesAutoresizingMaskIntoConstraints = NO;
Run Code Online (Sandbox Code Playgroud)
但是当我加载使用我的子类的注释的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.'
Run Code Online (Sandbox Code Playgroud)
我没有覆盖-layoutSubviews,所以对我看来,Apple的MKAnnotationView实现还没有为自动布局做好准备.有没有一些聪明的方法来解决MKAnnotationView中缺少自动布局支持所以我可以在我的子类中使用自动布局?
注释视图的AutoLayout工作正常,直到iOS SDK 8.使用iOS SDK 8,我看到具有动态约束的视图的奇怪行为,它们在整个地图上移动,有时甚至对齐左上角.
解决方案是setTranslatesAutoresizingMaskIntoConstraints:YES和
[annotationView setBounds:CGRectMake(0, 0, width, height)];
Run Code Online (Sandbox Code Playgroud)
并使用从子视图计算的宽度和高度.那么至少你可以使用子视图的约束.