iOS:Autolayout在iPhone中显示的大小不正确
我正在制作一个应用程序,其中我需要在底部显示两个名为“登录”和“登录”的按钮。我使用了Autolayout,因为我需要支持所有iPhone设备。它一直工作到iPhone5S,并且显示正确。然而在iPhone6中,注册按钮并没有扩大它的宽度。我做错了什么,但不知道在哪里。下面是我的代码 附加是屏幕截图也iOS:Autolayout在iPhone中显示的大小不正确,ios,objective-c,autolayout,Ios,Objective C,Autolayout,我正在制作一个应用程序,其中我需要在底部显示两个名为“登录”和“登录”的按钮。我使用了Autolayout,因为我需要支持所有iPhone设备。它一直工作到iPhone5S,并且显示正确。然而在iPhone6中,注册按钮并没有扩大它的宽度。我做错了什么,但不知道在哪里。下面是我的代码 附加是屏幕截图也 NSDictionary *viewsDictionary = @{@"loginButton":self.loginButton}; //Define the LoginButton Size
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];
您已将登录按钮和注册按钮的大小分别固定为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];
谢谢你,伙计,你是冠军,这真的帮了我很大的忙,再次感谢我+1。