gpt4 book ai didi

iOS:Autolayout 在 iPhone 中未显示正确的尺寸

转载 作者:搜寻专家 更新时间:2023-10-30 20:17:04 24 4
gpt4 key购买 nike

我正在制作一个应用程序,我需要在其中显示两个名为“登录”和“登录”的按钮。我为此使用了 Autolayout,因为我需要支持所有 iPhone 设备。它一直工作到 iPhone 5S 并显示正确。但是在 iPhone 6 中,注册按钮没有扩大其宽度。我做错了什么,但不确定在哪里。下面是我的代码。

附上截图。 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];

最佳答案

您已将登录按钮和注册按钮的大小固定为 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];

关于iOS:Autolayout 在 iPhone 中未显示正确的尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26602484/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com