iOS 以编程方式居中对象
iOS Programmatically Center Objects
我正在为 iPhone 6 Plus 支持更新应用程序。在以编程方式设置的其中一个视图中,运行 在 5S 或以下版本中所有视图都以它们应有的方式居中。然而,在 6 Plus 上,它略微向左移动。我如何调整此代码以使其在所有版本中都水平居中?
[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)];
您可以使用 "bounds" 和 "center":
self.logInView.usernameField.bounds = CGRectMake(0,0,250,50);
self.logInView.usernameField.center = CGPointMake(160, 150);
答案很简单……使用自动布局。但是如果你不知道如何使用它,试试这个:
首先,创建一些定义:
#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);
希望这个回答对您有所帮助! ;)
[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 水平井。
您似乎正在更新 Parse loginView。我遇到了同样的问题......让默认的 loginView 适应所有屏幕设备尺寸真的很痛苦。我将分享一些代码,希望对您有所帮助。这应该在 Parse 的 IOS Helper Page 上。 将其放入您的 .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] );
}
我正在为 iPhone 6 Plus 支持更新应用程序。在以编程方式设置的其中一个视图中,运行 在 5S 或以下版本中所有视图都以它们应有的方式居中。然而,在 6 Plus 上,它略微向左移动。我如何调整此代码以使其在所有版本中都水平居中?
[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)];
您可以使用 "bounds" 和 "center":
self.logInView.usernameField.bounds = CGRectMake(0,0,250,50);
self.logInView.usernameField.center = CGPointMake(160, 150);
答案很简单……使用自动布局。但是如果你不知道如何使用它,试试这个:
首先,创建一些定义:
#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);
希望这个回答对您有所帮助! ;)
[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 水平井。
您似乎正在更新 Parse loginView。我遇到了同样的问题......让默认的 loginView 适应所有屏幕设备尺寸真的很痛苦。我将分享一些代码,希望对您有所帮助。这应该在 Parse 的 IOS Helper Page 上。 将其放入您的 .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] );
}