Subclassing MKAnnotationView with use of auto layout

这一生的挚爱 提交于 2019-12-13 01:30:06

问题


I like the new auto layout functionality in iOS 6 but I've run into a bit of trouble when using it in combination with my MKAnnotationView subclass.

I've disabled the autoresizing mask translation in the initialization method.

self.translatesAutoresizingMaskIntoConstraints = NO;

But the app throws an NSInternalInconsistencyException when I load the MKMapView that uses an annotation of my subclass.

*** 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.'

I don't override -layoutSubviews so to me this looks like Apple's implementation of MKAnnotationView isn't ready for auto layout. Is there some smart way to get around the lack of auto layout support in MKAnnotationView so I can use auto layout in my subclass?


回答1:


AutoLayout for annotation views worked fine until iOS SDK 8. With iOS SDK 8 I see really strange behaviour of views with dynamic constraints, they are moving all over the map or sometimes even align top left.

Solution was setTranslatesAutoresizingMaskIntoConstraints:YES and

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

and use width and height calculated from the subviews. Then at least you can use constraints for the subviews.




回答2:


Don't disable the translatesAutoresizingMaskIntoConstraints of your ENMapAnnotationView (or you will miss the Pin). And make sure the enable the view which you want to add to the ENMapAnnotationView's translatesAutoresizingMaskIntoConstraints!!!

photoViewAddToAnnotationView.translatesAutoresizingMaskIntoConstraints = YES;

That's it! We don't need to turn off Auto layout (Turn off the Auto layout may solve the problem as well'




回答3:


Probably you have to implement the - (void)layoutSubviews method in your ENMapAnnotaitonView class.



来源:https://stackoverflow.com/questions/14591366/subclassing-mkannotationview-with-use-of-auto-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!