因此,我在 UIViewController
的不同元素上使用了 NSLayoutConstraints
,但由于某些原因它们影响了我的按钮。像这样:
这是我的代码:
// Twitter button
WelcomeButton *twitterButton = [[WelcomeButton alloc] initTwitterButtonWithFrame:CGRectMake(0, 0, 300, 60)];
twitterButton.center = CGPointMake(self.view.frame.size.width / 2, 350);
twitterButton.alpha = 1.0;
twitterButton.translatesAutoresizingMaskIntoConstraints = NO;
[twitterButton addTarget:self action:@selector(twitterSignUpButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:twitterButton];
// Facebook button
WelcomeButton *facebookButton = [[WelcomeButton alloc] initFacebookButtonWithFrame:CGRectMake(0, 0, 300, 60)];
facebookButton.center = CGPointMake(self.view.frame.size.width / 2, 0);
facebookButton.alpha = 1.0;
facebookButton.translatesAutoresizingMaskIntoConstraints = NO;
[facebookButton addTarget:self action:@selector(facebookSignUpButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:facebookButton];
// Email Sign-Up button
WelcomeButton *emailButton = [[WelcomeButton alloc] initEmailButtonWithFrame:CGRectMake(0, 0, 300, 60)];
emailButton.center = CGPointMake(self.view.frame.size.width / 2, 0);
emailButton.alpha = 1.0;
emailButton.translatesAutoresizingMaskIntoConstraints = NO;
[emailButton addTarget:self action:@selector(emailSignUpButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:emailButton];
// Signed-Up Message Label
UILabel *signedUpLabel = [[UILabel alloc] init];
signedUpLabel.frame = CGRectMake(0, 0, 150, 25);
signedUpLabel.center = CGPointMake(self.view.center.x - (signedUpLabel.frame.size.width / 3) + 10, 0);
signedUpLabel.textColor = [UIColor whiteColor];
signedUpLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:16];
signedUpLabel.text = @"Already Signed Up ?";
signedUpLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:signedUpLabel];
// Sign-In button
UIButton *signInButton = [UIButton buttonWithType:UIButtonTypeSystem];
signInButton.frame = CGRectMake(0, 0, 100, 25);
signInButton.center = CGPointMake(self.view.center.x + (signedUpLabel.frame.size.width / 3) + 10, 0);
signInButton.titleLabel.textColor = [UIColor whiteColor];
signInButton.titleLabel.font = [UIFont systemFontOfSize:16];
[signInButton setTitle:@"Sign In" forState:UIControlStateNormal];
signInButton.translatesAutoresizingMaskIntoConstraints = NO;
signInButton.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
[self.view addSubview:signInButton];
这是我的 AutoLayout 代码:
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[signedUpLabel]-10-|"
options:0
metrics:0
views:NSDictionaryOfVariableBindings(signedUpLabel)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[signInButton]-2-|"
options:0
metrics:0
views:NSDictionaryOfVariableBindings(signInButton)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[emailButton]-200-[signedUpLabel]"
options:0
metrics:0
views:NSDictionaryOfVariableBindings(emailButton, signedUpLabel)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[facebookButton]-100-[emailButton]"
options:0
metrics:0
views:NSDictionaryOfVariableBindings(facebookButton, emailButton)]];
这里出了什么问题?
我希望它们在一个结构中对齐在一起,但为什么它们会这样显示?当我没有指定任何约束时,它们会正常显示...
最佳答案
你必须添加Horizontal Constraint
如果您想以编程方式进行,也许您可以使用:https://github.com/jrturton/UIView-Autolayout
编辑:
您可以添加一个 Height Constraint
,例如:
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[myView(==60)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(myButton)]];
关于ios - UIButton 在其他元素上使用 AutoLayout 时自动调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21883898/