簡體   English   中英

iOS:Autolayout在iPhone中沒有顯示正確的大小

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

我正在創建一個應用程序,我需要在底部顯示兩個名為登錄和登錄的按鈕。 我已經使用了Autolayout,因為我需要支持所有的iPhone設備。 它一直工作到iPhone 5S並顯示正確。但是在iPhone 6注冊按鈕沒有擴展其寬度。 我做錯了什么,但不知道在哪里。 以下是我的代碼。

附件也是截圖。 在此輸入圖像描述

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM