简体   繁体   English

iOS:Autolayout在iPhone中没有显示正确的大小

[英]iOS: Autolayout not showing correct size in iPhone

I am making one app, in which I need to show two buttons at bottom named Sign In and Login. 我正在创建一个应用程序,我需要在底部显示两个名为登录和登录的按钮。 I have used Autolayout for this, beacuase I need to support all iPhone device. 我已经使用了Autolayout,因为我需要支持所有的iPhone设备。 It's working till iPhone 5S and showing correct.However in iPhone 6 Sign Up button is not expanding its width. 它一直工作到iPhone 5S并显示正确。但是在iPhone 6注册按钮没有扩展其宽度。 I am doing something wrong, but not sure where. 我做错了什么,但不知道在哪里。 Below is my code. 以下是我的代码。

Attach is the screenshot also. 附件也是截图。 在此输入图像描述

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];

you have fixed login button and signup button size to 160 each. 你有固定的登录按钮和注册按钮大小为160。 thats the only issue. 这是唯一的问题。

You need to calculate screen width and acordingly give width constraint of the buttons. 您需要计算屏幕宽度并按顺序给出按钮的宽度约束。

Below code will resolve your issue. 下面的代码将解决您的问题。

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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM