2013-10-30 31 views
3

我正在開發ViewController,其中一些UI元素爲SubViews,但是當我在不同設備上運行應用時,這些元素存在問題。我正在通過代碼做所有事情。我應該使用AutoLayout?我應該如何使用這個元素的autolayout?如何在不同屏幕的正確位置放置子視圖

iphone5 (4 inches)

iphone 4 (3.5 inches)

我有以下項目:

titleLbl = [[WPCustomLabel alloc] initWithFrame:CGRectMake(30, self.view.frame.size.height/2, 260, 25)]; 
    [titleLbl setFont:[UIFont systemFontOfSize:18]]; 
    [self.view addSubview:titleLbl]; 

    NSLog(@"%f", self.view.bounds.size.height/2); 


    textLbl = [[WPCustomLabel alloc] initWithFrame:CGRectMake(40, self.view.frame.size.width/2 + 10, 250, 230)]; 
    [textLbl setTextAlignment:NSTextAlignmentJustified]; 
    [self.view addSubview:textLbl]; 


    omitBtn = [[WPCustomButton alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height-108 , self.view.bounds.size.width/2, 44)]; 
    [omitBtn setTitle:@"Omitir" forState:UIControlStateNormal]; 
    [omitBtn addTarget:self action:@selector(omitInfo) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:omitBtn]; 


    nextBtn = [[WPCustomButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height-108, self.view.frame.size.width/2, 44)]; 
    [nextBtn setTitle:@"Siguiente" forState:UIControlStateNormal]; 
    [nextBtn addTarget:self action:@selector(omitInfo) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:nextBtn]; 
+0

您是否使用AutoLaoyouts嘗試過故事板? –

+0

我不使用故事板.. – croigsalvador

+0

根據需要重寫'viewWillLayoutSubviews'方法並基於'self.view'的幀更新幀。 – rmaddy

回答

-1

如果您使用的故事板,然後是的,你應該使用自動佈局。只需在標籤和超級視圖的底部之間設置一個約束即可。

enter image description here

如果你不使用的故事板,您可以覆蓋viewWillLayoutSubviews並設置根據您的視圖的大小布局。

-2

我個人給建議不使用自動佈局,但用這個代碼

if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) { 
    if ([[UIScreen mainScreen] bounds].size.height == 480) { 
     //iPhone 4 
    }else{ 
     //iPhone 
    } 
}else{ 
    //iPad 
} 

這個代碼放到單獨定義的所有對象框。

+0

您更喜歡使用此代碼更改框架位置,不是嗎? – croigsalvador

+0

雅,我定義框架爲4和5.在我的觀點自動佈局在較低的iOS版本中創建一些時間問題。所以,我更喜歡.. –

+0

如果你只是針對iOS 6,然後自動佈局是更好的.... –

相關問題