Arun Kumar
Arun Kumar

Reputation: 491

Align UIButtons horizontally using Auto layout (programmatically)

I'm trying to place 2 UIButtons horizontally but no luck

  1. I'm using auto layout constraints programmatically.
  2. I don't want to put them in a container and align center to that container.

    NSDictionary *metrics = @{@"kLeftPadding":@(kLeftPadding),
                              @"kRightPadding":@(kRightPadding),
                              @"kMiddlePadding":@(kMiddlePadding),
                              @"kBottomPadding":@(kBottomPadding)};
    NSDictionary *constrainedViews = @{@"titleLabel": self.titleLabel,
                                       @"btnToday" : self.btnToday,
                                       @"btnPickaDay" : self.btnPickaDay,
                                       @"dividerView" : dividerView};
    NSArray *contraints = @[@"V:|-[titleLabel]-kBottomPadding-[btnToday(==40)]-[btnPickaDay(==btnToday)]-kBottomPadding-[dividerView]-0-|",
                            @"H:|-kLeftPadding-[titleLabel]-|",
                            @"H:|-kLeftPadding-[btnToday]-kMiddlePadding-[btnPickaDay(==btnToday)]-kRightPadding-|",
                            @"H:|-0-[dividerView]-0-|"];
    

second button starts from where first ends but in next line, not on same.

Upvotes: 1

Views: 312

Answers (1)

Dhaivat Vyas
Dhaivat Vyas

Reputation: 2928

I've tried your code but it's getting crashed so as per your question you simply need to write below code.

UILabel *lblText = [UILabel new];
[lblText setText:@"Some Title Text"];
[lblText setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:lblText];

UIButton *btnRed = [UIButton new];
[btnRed setBackgroundColor:[UIColor redColor]];
[btnRed setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:btnRed];

UIButton *btnGreen = [UIButton new];
[btnGreen setBackgroundColor:[UIColor greenColor]];
[btnGreen setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:btnGreen];

UIView *vwLine = [UIView new];
[vwLine setBackgroundColor:[UIColor blueColor]];
[vwLine setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:vwLine];


NSDictionary *dictViews = @{@"red":btnRed,@"green":btnGreen,
                            @"line":vwLine,@"lbl":lblText};

NSDictionary *dictMetrics = @{@"height":@(40),@"offset":@(-40)};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[red]-0-[green(==red)]-20-|" options:0 metrics:nil views:dictViews]];

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

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

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(height)-[lbl]-(height)-[red(height)]-(offset)-[green(==red)]-50-[line(2)]" options:0 metrics:dictMetrics views:dictViews]];

result:

enter image description here

Hope it will help you solve your problem. If any more query do let me know.

Upvotes: 2

Related Questions