2015-01-11 38 views
3

我正在更新iPhone 6 Plus支持的應用程序。在以編程方式設置的其中一個視圖上,在5S或更低版本上運行時,所有視圖都以應有的方式爲中心。然而,在6 Plus上,它略微向左滑動。如何調整此代碼以使其在所有版本中保持水平居中?iOS以編程方式中心對象

[self.logInView.usernameField setFrame:CGRectMake(35.0f, 125.0f, 250.0f, 50.0f)]; 
[self.logInView.passwordField setFrame:CGRectMake(35.0f, 175.0f, 250.0f, 50.0f)]; 
[self.fieldsBackground setFrame:CGRectMake(35.0f, 125.0f, 250.0f, 100.0f)]; 
[self.logInView.logInButton setFrame:CGRectMake(35.0f, 235.0f, 250.0f, 40.0f)]; 
+1

基地的幀座標(x和y)關閉父視圖的框架,而不是硬編碼值。 – rmaddy

+3

或使用Autolayout。 – Abizern

+0

您可能需要在viewDidLayoutSubviews中設置中心。此時你會知道父母的真實框架。 –

回答

3
[self.logInView.usernameField setFrame:CGRectMake(self.view.center.x - 125, 0, 250.0f, 50.0f);]; 
[self.logInView.passwordField setFrame:CGRectMake(self.view.center.x - 125, 175.0f, 250.0f, 50.0f)]; 
[self.fieldsBackground setFrame:CGRectMake(self.view.center.x - 125, 125.0f, 250.0f, 100.0f)]; 
[self.logInView.logInButton setFrame:CGRectMake(self.view.center.x - 125, 235.0f, 250.0f, 40.0f)]; 

您可以使用此代碼在所有iOS設備上獲得平躺的UI。

-3

您可以使用 「邊界」 和 「中心」:

self.logInView.usernameField.bounds = CGRectMake(0,0,250,50); 
self.logInView.usernameField.center = CGPointMake(160, 150); 
+1

但是,這如何確保視圖集中在差異設備上?你不能硬編碼座標。 – rmaddy

+0

只需使用[UIScreen mainScreen]邊界] .size.height/2和[UIScreen mainScreen]邊界] .size.width/2根據屏幕大小對其進行居中。 – Thorsten

1

答案很簡單...... 使用自動佈局。但是,如果你不知道如何使用它,試試這個:

首先,創建一些定義:

#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) 
#define IS_IPADDEVICE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
#define IS_IPHONE_4 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 480.0) 
#define IS_IPHONE_5 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0) 
#define IS_IPHONE_6 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0) 
#define IS_IPHONE_6_PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0) 
#define IS_IPAD (IS_IPADDEVICE && [[UIScreen mainScreen] bounds].size.height == 1024.0) 

然後創建一個簡單的if語句:

if (IS_IPHONE_4) { 
     // iPhone 4 
    } else if (IS_IPHONE_5) { 
     // iPhone 5 
    } else if (IS_IPHONE_6) { 
     // iPhone 6 
    } else if (IS_IPHONE_6_PLUS) { 
     // iPhone 6 plus 
    } else if (IS_IPAD) { 
     // iPad 
    } else { 
     // other device 
    } 

如果我想要居中對象我使用frame方法:

theobject.frame = CGRectMake((theobject.frame.size.width/2) - (theobject.image.size.width/2), 20, theobject.image.size.width, theobject.image.size.height); 

我希望這個答案你會幫助你的! ;)

+0

我嘗試這樣做,並且收到很多錯誤,說'未聲明使用IS_IPHONE_4'等。 – user717452

+0

即使沒有自動佈局,也沒有任何設備宏的原因。答案中的最後一行代碼應該基於父視圖的框架。 – rmaddy

+0

您是否在進口下面創建了您的定義:http://cl.ly/image/2L3B3I1b3s13 ??? –

0

看來你正在更新你的解析loginView。我遇到了同樣的問題......讓默認的loginView適合所有屏幕設備尺寸真的很痛苦。我會分享一些我希望能幫到的代碼。這應該在Parse的IOS幫助頁面上。 將這個在您的.m文件

- (void)viewDidLayoutSubviews { 
    [super viewDidLayoutSubviews]; 

//Returns the screen dimensions (in points) of the user's current device 
CGRect screenBounds = [[UIScreen mainScreen] bounds]; 


//Finds to see if the user has an iPhone 4s, and then set's the layout if they do 
if(screenBounds.size.height==480) 
{ 
    [self.logInView.logInButton setFrame:CGRectMake(self.view.center.x - 125, 300.0f, 250.0f, 50.0f)]; 
    [self.logInView.usernameField setFrame:CGRectMake(self.view.center.x - 125, 165.0f, 250.0f, 50.0f)]; 
    [self.logInView.passwordField setFrame:CGRectMake(self.view.center.x - 125, 215.0f, 250.0f, 50.0f)]; 
    [self.logInView.passwordForgottenButton setFrame:CGRectMake(self.view.center.x - 125, 265.0f, 250.0f, 40.0f)]; } 


//Finds to see if the user has an iPhone 5/5s, and then set's the layout if they do 
else if (screenBounds.size.height == 568) 
{ 
    [self.logInView.logInButton setFrame:CGRectMake(self.view.center.x - 125,360.0f, 250.0f, 50.0f)]; 
    [self.logInView.usernameField setFrame:CGRectMake(self.view.center.x - 125, 245.0f, 250.0f, 50.0f)]; 
    [self.logInView.passwordField setFrame:CGRectMake(self.view.center.x - 125, 295.0f, 250.0f, 50.0f)]; 
    [self.logInView.passwordForgottenButton setFrame:CGRectMake(self.view.center.x - 125, 445.0f, 250.0f, 40.0f)]; } 

//If the user doesn't have a 4S/5/5s, then they must be using an iPhone 6/6+ 
else { 

     [self.logInView.logInButton setFrame:CGRectMake(self.view.center.x - 125, 420.0f, 250.0f, 50.0f)]; 
     [self.logInView.usernameField setFrame:CGRectMake(self.view.center.x - 125, 275.0f, 250.0f, 50.0f)]; 
     [self.logInView.passwordField setFrame:CGRectMake(self.view.center.x - 125, 325.0f, 250.0f, 50.0f)]; 
     [self.logInView.passwordForgottenButton setFrame:CGRectMake(self.view.center.x - 125, 485.0f, 250.0f, 40.0f)]; 

//Prints out the current device being used 
    NSLog(@"Current device: %@", [[UIDevice currentDevice] model]); 
}