2014-10-28 45 views
3

我正在制作一个应用程序,在该应用程序中,我需要在底部显示名为登录和登录的两个按钮。我已经使用Autolayout为此,我需要支持所有的iPhone设备。它一直工作到iPhone 5S并显示正确。但是在iPhone 6注册按钮没有扩大其宽度。我做错了什么,但不知道在哪里。以下是我的代码。iOS:Autolayout在iPhone中未显示正确的大小

附加也是屏幕截图。 enter image description here

NSDictionary *viewsDictionary = @{@"loginButton":self.loginButton}; 

//Define the LoginButton Size 

NSArray *constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[loginButton(52)]" options:0 metrics:nil views:viewsDictionary]; 
NSArray *constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[loginButton(160)]" options:0 metrics:nil views:viewsDictionary]; 

[self.loginButton addConstraints:constraint_H]; 
[self.loginButton addConstraints:constraint_V]; 

//Define the LoginView Postion 

NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[loginButton]-0-|" options:0 metrics:nil views:viewsDictionary]; 
NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[loginButton]" options:0 metrics:nil views:viewsDictionary]; 

[self.view addConstraints:constraint_POS_V]; 
[self.view addConstraints:constraint_POS_H]; 

//Define the SignInButton Size 

NSDictionary *yellowDictionary = @{@"signInButton":self.signInButton}; 

NSArray *yellow_constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[signInButton(52)]" options:0 metrics:nil views:yellowDictionary]; 
NSArray *yellow_constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:@"H:[signInButton(160)]" options:0 metrics:nil views:yellowDictionary]; 

[self.signInButton addConstraints:yellow_constraint_H]; 
[self.signInButton addConstraints:yellow_constraint_V]; 


//Define the SignInView Postion 

NSArray *yellowconstraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[signInButton]-0-|" options:0 metrics:nil views:yellowDictionary]; 
NSArray *yellowconstraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-161-[signInButton]" options:0 metrics:nil views:yellowDictionary]; 

[self.view addConstraints:yellowconstraint_POS_V]; 
[self.view addConstraints:yellowconstraint_POS_H]; 

回答

2

您已将固定登录按钮和注册按钮大小设置为每个160。这是唯一的问题。

您需要计算屏幕宽度,并按照给出的按钮的宽度约束。

下面的代码将解决您的问题。

CGSize screenSize = [UIScreen mainScreen].bounds.size;

NSArray *constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[loginButton(52)]" options:0 metrics:nil views:viewsDictionary]; 
NSArray *constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:[loginButton(%.f)]",screenSize.width/2] options:0 metrics:nil views:viewsDictionary]; 

[self.loginButton addConstraints:constraint_H]; 
[self.loginButton addConstraints:constraint_V]; 

//Define the LoginView Postion 

NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[loginButton]-0-|" options:0 metrics:nil views:viewsDictionary]; 
NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[loginButton]" options:0 metrics:nil views:viewsDictionary]; 

[self.view addConstraints:constraint_POS_V]; 
[self.view addConstraints:constraint_POS_H]; 

//Define the SignInButton Size 

NSDictionary *yellowDictionary = @{@"signInButton":self.signInButton}; 

NSArray *yellow_constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[signInButton(52)]" options:0 metrics:nil views:yellowDictionary]; 
NSArray *yellow_constraint_V = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:[signInButton(%.f)]",screenSize.width/2] options:0 metrics:nil views:yellowDictionary]; 

[self.signInButton addConstraints:yellow_constraint_H]; 
[self.signInButton addConstraints:yellow_constraint_V]; 


//Define the SignInView Postion 

NSArray *yellowconstraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[signInButton]-0-|" options:0 metrics:nil views:yellowDictionary]; 
NSArray *yellowconstraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-%f-[signInButton]",(screenSize.width/2)+1] options:0 metrics:nil views:yellowDictionary]; 

[self.view addConstraints:yellowconstraint_POS_V]; 
[self.view addConstraints:yellowconstraint_POS_H]; 
+0

感谢的人,你从我身边都是冠军,这真的帮了我很多,再次感谢+1。 – Kashif 2014-10-28 07:12:38

+0

欢迎@Kashif – 2014-10-28 07:18:26

相关问题