IOS: Auto detect not showing on iPhone
I am creating one application where I need to show two buttons at the bottom named Sign In and Login. I used Autolayout for this, beacuase I need to support all iPhone devices. It works up to iPhone 5S and displays correctly. However, the iPhone 6 Sign Up does not expand its width. I am doing something wrong, but I don't know where. Below is my code.
Attach a screenshot as well.
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];
+3
source to share
1 answer
you have a fixed login button and register button size up to 160 each. that's the only problem.
You need to calculate the width of the screen and limit the width of the buttons accordingly.
Below is the solution to the problem.
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];
+2
source to share