1

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];


 }

私は何を間違っていますか?

4

1 に答える 1

2

いくつかのこと:

1) MKMapViewxib ファイルに自動サイズ設定がありますか? これは、コードで行っていることを妨げる可能性があります。

2) ではなく、正しい場所でローテーション ロジックを処理する必要がありますviewDidLoad。UIViewController Class referenceで、 Handling View Rotationsの部分をチェックして、コードを配置する場所を確認します。

于 2012-10-08T12:07:49.020 に答える