ios – NSLayoutConstraint和MKMapView

ios – NSLayoutConstraint和MKMapView,第1张

概述在我的viewDidLoad方法中,我有代码来创建MKMapView,一些约束和一个UIToolbar: 我有一个MKMapView: MKMapView *mapView = [[MKMapView alloc] init];[mapView setTranslatesAutoresizingMaskIntoConstraints:NO];mapView.userInteractionEna 在我的vIEwDIDLoad方法中,我有代码来创建MKMapVIEw,一些约束和一个UIToolbar:

我有一个MKMapVIEw:

MKMapVIEw *mapVIEw = [[MKMapVIEw alloc] init];[mapVIEw setTranslatesautoresizingMaskIntoConstraints:NO];mapVIEw.userInteractionEnabled = TRUE;mapVIEw.showsUserLocation = TRUE;mapVIEw.mapType = MKMapTypeHybrID;mapVIEw.delegate = self;[mapVIEw setUserTrackingMode:MKUserTrackingModeFollow animated:YES];[self.vIEw addSubvIEw:mapVIEw];

我创建了2个约束来使其全屏显示:

NSMutableArray *constraints = [NSMutableArray array];[constraints addobjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[mapVIEw]|"                                                                         options:0                                                                         metrics:nil                                                                           vIEws:NSDictionaryOfVariableBindings(mapVIEw)]];[constraints addobjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|[mapVIEw]|"                                                                         options:0                                                                         metrics:nil                                                                           vIEws:NSDictionaryOfVariableBindings(mapVIEw)]];[self.vIEw addConstraints:constraints];

效果很好.但是当我尝试向地图视图添加任何内容时:

UIToolbar *topbar = [[UIToolbar alloc ]initWithFrame:CGRectMake(0,self.vIEw.frame.size.wIDth,44)];[topbar setTranslatesautoresizingMaskIntoConstraints:NO];topbar.barStyle = UIbarStyleBlackTranslucent;[mapVIEw addSubvIEw:topbar];

它会抛出一个错误:

*** Assertion failure in -[MKMapVIEw layoutSublayersOfLayer:],/SourceCache/UIKit_Sim/UIKit-2372/UIVIEw.m:57762013-01-10 10:24:17.503 Landscout 2[2001:14003] *** Terminating app due to uncaught exception 'NSInternalinconsistencyException',reason: 'auto Layout still required after executing -layoutSubvIEws. MKMapVIEw's implementation of -layoutSubvIEws needs to call super.'

根据我的理解,由于我要在地图视图中添加新视图,因此地图视图需要重新计算其中的所有约束吗?基本上是一个用于约束的drawRect方法.

我怎样才能解决这个问题?

解决方法 我通过简单地使用内容视图填充所有内容来解决问题.我没有将工具栏添加到myMapVIEw,而是将其添加到myContentVIEw.

我很确定MKMapVIEw还没有设置为使用约束. MapVIEw不会调用[super layoutSubvIEws],这最终会导致[UIVIEw layoutSubvIEws].

我想到的另一个可能的解决方案是向MKMapVIEw添加一个类别,它将覆盖-layoutSubvIEw以调用[super layoutSubvIEw],但我不知道如何找出[MKMapVIEw layoutSubvIEw]中我还需要的其他内容加.

总结

以上是内存溢出为你收集整理的ios – NSLayoutConstraint和MKMapView全部内容,希望文章能够帮你解决ios – NSLayoutConstraint和MKMapView所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1064511.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存