ios - mapView的框架错误

标签 ios uiview mkmapview

我在连接到 Outlet 的 xib 中有一个 MapView。我通过代码为肖像和风景设置框架。框架是正确的,但输出是错误的。为什么?

这是我在 MapViewController.h 中的代码:

  @interface MapViewController : UIViewController <MKMapViewDelegate>{

  }

  @property (nonatomic, strong)UIImageView * imageView;
  @property (nonatomic, strong)IBOutlet UIButton * buttonHome;
  @property (nonatomic, strong)IBOutlet UIButton * buttonMap;
  @property (nonatomic, strong)IBOutlet UIButton * buttonFavorites;
  @property (nonatomic, strong)IBOutlet UILabel * labelTitle;
  @property (nonatomic, strong)IBOutlet MKMapView *mapView;

和MapViewController.m

  @implementation MapViewController

  @synthesize imageView;
  @synthesize buttonFavorites;
  @synthesize buttonHome;
  @synthesize buttonMap;
  @synthesize labelTitle;
  @synthesize mapView;

  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
 }

 -(void) layoutPortrait {

self.imageView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bar_V.png"]];
self.imageView.frame= CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width, 80);
self.imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.labelTitle.frame =CGRectMake(14, 83, 121, 54);
self.buttonFavorites.frame= CGRectMake(109, 485, 33, 33);
self.buttonHome.frame =CGRectMake(14, 485, 33, 33);
self.buttonMap.frame=CGRectMake(61, 485, 33, 33);
self.mapView.frame = CGRectMake(0, 140, 320, 310);


}

-(void) layoutLandscape {

self. imageView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bar_O.png"]];
self.imageView.frame= CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y,self.view.bounds.size.width,70 );
self.imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.labelTitle.frame =CGRectMake(20,72,140,48);

self.buttonFavorites.frame= CGRectMake(107,237,33,33);
self.buttonHome.frame =CGRectMake(11,237,33,33);
self.buttonMap.frame=CGRectMake(59,237,33,33);
self.mapView.frame = CGRectMake(201,65,367,227);


}


 - (void)viewDidLoad
{
[super viewDidLoad];


UIInterfaceOrientation deviceOrientation= self.interfaceOrientation;

if(deviceOrientation == UIInterfaceOrientationPortrait || deviceOrientation== UIInterfaceOrientationPortraitUpsideDown) {
    [self layoutPortrait];

}
else if (deviceOrientation== UIInterfaceOrientationLandscapeRight){
    [self layoutLandscape];

}
else if (deviceOrientation==UIInterfaceOrientationLandscapeLeft){

    [self layoutLandscape];

}

[self.view addSubview:self.imageView];
[self.view addSubview:self.mapView];
[self.view addSubview:self.labelTitle];
[self.view addSubview:self.buttonHome];
[self.view addSubview:self.buttonMap];
[self.view addSubview:self.buttonFavorites];


self.mapView.delegate=self;
}

 -(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice]orientation];

if(self.imageView){
    [self.imageView removeFromSuperview];

}

if(deviceOrientation == UIInterfaceOrientationPortrait || deviceOrientation== UIInterfaceOrientationPortraitUpsideDown) {
    [self layoutPortrait];

}
else if (deviceOrientation== UIInterfaceOrientationLandscapeRight){
    [self layoutLandscape];

}
else if (deviceOrientation==UIInterfaceOrientationLandscapeLeft){

    [self layoutLandscape];

}

[self.view addSubview:self.mapView];
[self.view addSubview:self.labelTitle];
[self.view addSubview:self.imageView];
[self.view addSubview:self.buttonFavorites];
[self.view addSubview:self.buttonHome];
[self.view addSubview:self.buttonMap];


 }

我做错了什么?

最佳答案

一些事情:

1) 你的 xib 文件中的 MKMapView 有自动调整大小吗?这可能会干扰您在代码上所做的事情。

2) 您应该在正确的位置处理旋转逻辑,而不是在 viewDidLoad 中。关于UIViewController Class reference ,检查部分Handling View Rotations,这样您就可以看到将代码放在哪里。

关于ios - mapView的框架错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12781299/

相关文章:

ios - 在 View 变换(旋转)和调整大小(到边界)后保持自动布局

iphone - 在 MKMap View 中显示附近的餐馆

ios - 错误 "Property ' 在类型 'MyAnnotation' 的对象上找不到标题“

ios - 使用 BringSubviewToFront 时图像回弹

ios - 无法保持选择表行

ios - 在具有透明 View 的 UITableview 中显示图像列表

ios - subview Controller 的 View 未显示在容器中

swift - 如何添加横幅广告添加到弹出 View swift 4

ios - 我是否需要为不同类型的注释创建实现 MKAnnotation 协议(protocol)的类?

ios - 当 iOS 中的 RTL 切换时,UIAlertController 不在中心显示文本