2014-11-04 47 views
1

当我在地图下方创建故事板时,会占用整个屏幕。这是可以理解的,我用下面的代码来使用视图的整个边界。但是,如果我做一个正常的“初始化”,我会失去地图的功能!地图通常会放大我的位置和其他一些东西。当我使用init而不是initWithFrame时,地图不会到达我的位置。MKMapView的错误

self.myMapView =[[MKMapView alloc]initWithFrame:self.view.bounds]; 

self.myMapView.delegate=self; 

如何初始化地图而不需要像我的故事板中显示的那样接管整个屏幕。 iOS新手,感谢您的帮助!

Capture of StoryBoard

//Setting up the location manger for finding the User's location 
self.locationManager = [[CLLocationManager alloc]init]; 
self.locationManager.delegate = self; 

//Check to see if they have Authorization to use to use location servieces 
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) 
{ 
    [self.locationManager requestAlwaysAuthorization]; 
} 

    [self.locationManager startUpdatingLocation]; 


//Initialize the map and specifiy bounds 
self.myMapView =[[MKMapView alloc]initWithFrame:self.view.bounds]; 

//Set the delegate to itself 
self.myMapView.delegate=self; 

//Set a standard mapview, Enable scrolling and zooming 
self.myMapView.mapType = MKMapTypeStandard; 
self.myMapView.scrollEnabled = YES; 
self.myMapView.zoomEnabled = YES; 
//specifcy resizing 
self.myMapView.autoresizingMask = UIViewAutoresizingFlexibleWidth |  UIViewAutoresizingFlexibleTopMargin; 

//show the User's location and set tracking mode 
self.myMapView.showsUserLocation = YES; 
self.myMapView.userTrackingMode = MKUserTrackingModeFollow; 

//add the VIEW!! 
[self.view addSubview:self.myMapView]; 

//Set up the UISearch Bar 
self.mySearch = [[UISearchBar alloc]init]; 
self.mySearch.delegate = self; 

//Set the initial Text 
self.mySearch.prompt = @"Enter Address or Use Current Location"; 
+0

你的问题不清楚。你想在故事板中添加地图视图还是在代码中创建地图视图?你不应该这样做。 – rdelmar 2014-11-04 01:18:12

+0

原谅我我是iOS新手,我将MKMapView拖放到视图控制器上,我相信它是在viewDidLoad方法的代码中创建的。我已经添加了我的其他代码。 – tennis779 2014-11-04 01:35:06

+0

由于您已将它添加到故事板中的控制器,因此您不应该在代码中创建一个。只需在故事板中设定想要的尺寸,并为其添加适当的约束即可。 – rdelmar 2014-11-04 05:11:20

回答

1

有创建地图视图两种方法。

1.创建代码。如你的例子所示。您需要设置地图视图框架。如果您首先需要创建UISearchBar,则应根据搜索栏位置设置地图视图框架。它可能看起来像这样。

self.mySearch = [[UISearchBar alloc] init]; 
self.mySearch.frame = CGRectMake(0, 0, self.view.frame.size.width, 40); 
//... 
self.myMapView = [[MKMapView alloc] init]; 
self.myMapView.frame = CGRectMake(0, self.mySearch.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - self.mySearch.frame.size.height); 
//... 

2.更简单的方法 - 创建故事板。首先,不需要在代码中创建您的网点(例如搜索栏和地图视图)。您只需简单拖动即可拖放所有要查看的控件。打开故事板,将视图控制器类从UIViewController设置为您的类名称。

注意的属性必须是IBOutlets

@property (nonatomic,weak) IBOutlet MKMapView *myMapView; 
@property (nonatomic,weak) IBOutlet UISearchBar *mySearchBar; 

下探您的控件来查看后,必须可视控件连接到你的类网点。看看这个连接被解释的tutorial

+0

谢谢你的工作 – tennis779 2014-11-04 14:39:20