0

我在 iPhone 的代码集中有一个通用应用程序和子视图的框架,我看到将 iPad 模拟器放置的子视图大小相同。因此 iPad 我必须重写所有框架或是否有快捷方式?这是代码。

 -(void) layoutPortrait {

self.imageView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bar_V.png"]];
self.imageView.frame= CGRectMake(0, 0, 320, 80);
self.labelTitle.frame =CGRectMake(14, 103, 91, 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);


 }

 -(void) layoutLandscape {

self. imageView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bar_O.png"]];
self.imageView.frame= CGRectMake(0,0,568,65 );
self.labelTitle.frame =CGRectMake(20,92,165,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);



 }


 - (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.hidden=YES;
// Do any additional setup after loading the view from its nib.

//orientazioni
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.labelTitle];
[self.view addSubview:self.imageView];
[self.view addSubview:self.buttonFavorites];
[self.view addSubview:self.buttonHome];
[self.view addSubview:self.buttonMap];
}
4

1 回答 1

1

有两种方法可以解决这个问题。

首先,您可以创建特定于 iPad 和 iPhone 5 的框架。

其次,您使视图根据父视图或其他子视图自动调整大小。这是通过自动调整大小完成的。一个很好的链接,描述了这一点:iOS 5 iPhone Rotation, View Resizing and Layout Handling

我注意到您的代码使用了旧的视图布局方式,即检测界面方向,然后根据该方式布局框架。更好的方法,自 iOS 6 以来被大力强调,是使用 layoutSubviews() 和同伴。

于 2012-10-08T08:21:22.107 回答