Objective-C - 为什么按钮没有对齐底部中心?

Objective-C - why the button is not aligned bottom center?

我的按钮底部没有居中对齐?设置为全屏:

使用自动布局设置为底部居中:

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib.
  UIButton *but= [UIButton buttonWithType:UIButtonTypeRoundedRect];
  [but addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
  int startHeight = 167;
  int frameHeight = self.view.frame.size.height - startHeight;
  [but setFrame:CGRectMake(0, startHeight, 320, frameHeight)];
  [but setTitle:@"Login" forState:UIControlStateNormal];
  [but setExclusiveTouch:YES];
  [but setImage:[UIImage imageNamed:@"Image"] forState:UIControlStateNormal];
  [self.view addSubview:but];


  NSDictionary *viewsDict = @{@"but" : but};
  [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-167-[but]-0-|"
                                                                    options:0 metrics:nil views:viewsDict]];
  [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[but]-0-|"
                                                                    options:0 metrics:nil views:viewsDict]];
}


-(void) buttonClicked:(UIButton*)sender {
  NSLog(@"you clicked on button %@", sender.tag);
}

- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
  // Dispose of any resources that can be recreated.
}


@end

Set to bottom center with auto layout

没有。您没有对任何事物的中心设置任何限制。

而不是使用

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[but]-0-|"
options:0 metrics:nil views:viewsDict]];

使用

self.view.addConstraint(NSLayoutConstraint(item: but, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1.0, constant: 0))

编辑

已实现的 OP 代码是 Objective - C

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:but attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];

编辑

这是我在 ViewDidLoad 中的代码

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *but = [[UIButton alloc] init];
    [but setTitle:@"ABCD" forState:UIControlStateNormal];
    [but setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    int startHeight = 167;
    int frameHeight = self.view.frame.size.height - startHeight;
    [but setFrame:CGRectMake(0, startHeight, 320, frameHeight)];
    
    [but setTranslatesAutoresizingMaskIntoConstraints:false];
    [self.view addSubview:but];
    NSDictionary *viewsDict = @{@"button" : but};
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[button]-0-|"
                                                                      options:0 metrics:nil views:viewsDict]];
    NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:but attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
    constraint.active = true;
    [self.view addConstraint:constraint];
    [self.view layoutIfNeeded];
    // Do any additional setup after loading the view, typically from a nib.
}

这是输出:)

很有魅力:)请检查您缺少什么声明??

您的代码中的错误:

  1. 您错过了“[but setTranslatesAutoresizingMaskIntoConstraints:false];

当您应用自动布局约束时,您通知 iOS 不要将默认的自动调整大小掩码转换为自动布局约束,因为它可能与您应用的自动布局约束冲突:)

2.constraint.active = true;

创建约束后确保激活它,正如 EmilioPelaez 在评论中完美指出的那样:)