2014-11-04 20 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