首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MKMapView错误

MKMapView错误
EN

Stack Overflow用户
提问于 2014-11-04 01:14:55
回答 1查看 80关注 0票数 1

当我在地图下面创建故事板时,需要整个屏幕。这在下面的代码中是可以理解的,它使用视图的整个边界。然而,如果我做了一个正常的"init",我失去了地图的功能!地图通常会放大到我的位置,还有一些其他的东西。当我使用init而不是initWithFrame时,地图不会到达我的位置。

代码语言:javascript
复制
 self.myMapView =[[MKMapView alloc]initWithFrame:self.view.bounds];

 self.myMapView.delegate=self;

我如何初始化地图,而不让它接管整个屏幕,如我的故事板所示。新来的iOS,谢谢你的帮助!

代码语言:javascript
复制
//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";
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-04 10:15:00

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

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

代码语言:javascript
复制
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.更简单的方法--在故事板中创建。首先,不需要在代码中创建您的出口(例如搜索栏和地图视图)。简单地拖动‘n’,将所有控件拖放到视图中。打开故事板,将视图控制器类从UIViewController设置为类名。

注意,这些属性必须是IBOutlets

代码语言:javascript
复制
@property (nonatomic,weak) IBOutlet MKMapView *myMapView;
@property (nonatomic,weak) IBOutlet UISearchBar *mySearchBar;

删除要查看的控件后,必须将可视控件连接到类插座。看看这个教程,其中解释了连接。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26726193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档