我有這個視圖控制器設置在故事板上,我需要以編程方式添加一個MapView。視覺格式語言寬度和高度約束不被尊重
我想讓地圖填充視圖的寬度,並且在兩個方向上的高度均爲100。另外,我想要在地圖下方的imageView的間距爲10。
這是我正在使用的代碼。
_map = [MyClass sharedMap]; // Get singleton
[_map removeFromSuperview]; // Remove from the other VC view
[_map removeConstraints:[_map constraints]]; // Remove constraints if any
[[self view] addSubview:_map];
[[self view] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_map]|" options:0 metrics:nil views:@{@"_map" : _map}]];
[[self view] addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_map(100)]-10-[_sellerImage]" options:0 metrics:nil views:@{@"_map" : _map, @"_imageView" : _imageView}]];
但結果是:
- 寬度是恆定的並且不填充所述屏幕寬度
- 的高度增加時,它的上縱向朝向
- 10像素間隔的ImageView的是好吧
我需要爲地圖設置初始框架嗎?
此mapview是許多視圖中使用的單例,以節省內存。這是它的初始化代碼:
+ (MKMapView *)sharedMap {
static MKMapView *mapView;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
mapView = [[MKMapView alloc] init];
if (IS_IOS_7) {
[mapView setShowsUserLocation:YES];
[mapView setPitchEnabled:NO];
}
[mapView setAutoresizingMask:UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth];
});
return mapView;
}
很有可能。 – Pavan
在控制檯中是否有任何佈局警告? – jrturton
您可以在創建地圖視圖的位置添加代碼嗎? – jrturton