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

时间:2017-03-13 06:06:35

标签: ios objective-c iphone ipad autolayout

enter image description here

我的按钮底部中心不对齐?设为全屏:

enter image description here

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

#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

2 个答案:

答案 0 :(得分:3)

而不是使用

[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.
}

这是输出:)

enter image description here

像魅力一样工作:)请检查你遗失的是什么声明?

代码中的错误:

  1. 您错过了“[but setTranslatesAutoresizingMaskIntoConstraints:false];
  2. 当您应用自动布局约束时,您通知iOS不会将默认自动调整遮罩转换为自动布局约束,因为它可能与您应用的自动布局约束冲突:)

    2。constraint.active = true;

    创建约束后,请务必将其激活,正如EmilioPelaez在评论中所指出的那样:)

答案 1 :(得分:2)

  

使用自动布局设置到底部中心

没有。你没有对任何事情的中心设置任何约束。