2012-10-08 48 views
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]; 
} 

回答

1

有兩種方法可以解決這個問題。

首先,你可以創建專用於iPad的框架,而iPhone 5

其次,你讓意見根據父視圖自動調整,或到其他子視圖。這是通過自動調整來完成的。一個很好的鏈接描述這個:iOS 5 iPhone Rotation, View Resizing and Layout Handling

我注意到你的代碼使用佈局視圖的舊方式,即檢測接口的方向,然後根據這個佈局框架。更好的方式,這是自iOS 6以來強調,使用layoutSubviews()和同伴。