0

I'm trying in my custom UITableViewCell to get an equal space between all buttons. What I get is all buttons have a huge width and height. My goal is equal space and max size of 40x40 (only via code), but I don't know hot to do it.

My code so far:

- (void)addEqualSpaceToButtons
{
    if (_didAddEqualSpace) {
        return;
    }

    _didAddEqualSpace = YES;

    // 1
    self.btnCall.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnMessage.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnCalendar.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnMemo.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnForwardMessage.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnMore.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnDelete.translatesAutoresizingMaskIntoConstraints = NO;

    // 2
    NSLayoutConstraint *equalSpace = [NSLayoutConstraint constraintWithItem:self.btnDelete
                                                                  attribute:NSLayoutAttributeBottom
                                                                  relatedBy:NSLayoutRelationEqual
                                                                     toItem:self
                                                                  attribute:NSLayoutAttributeBottom
                                                                 multiplier:1
                                                                   constant:0];

    [self addConstraint:equalSpace];

    // 3
    NSDictionary *views = NSDictionaryOfVariableBindings(_btnDelete, _btnMore, _btnForwardMessage, _btnMemo, _btnCalendar, _btnMessage, _btnCall);
    NSArray *fixEqualSpace = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_btnDelete][_btnMore(==_btnDelete)][_btnForwardMessage(==_btnDelete)][_btnMemo(==_btnDelete)][_btnCalendar(==_btnDelete)][_btnMessage(==_btnDelete)][_btnCall(==_btnDelete)]|"
                                                                     options:NSLayoutFormatAlignAllBottom
                                                                     metrics:nil
                                                                       views:views];
    [self addConstraints:fixEqualSpace];
}
Idan Moshe
  • 1,675
  • 4
  • 28
  • 65
  • First, you haven't specified anything about the maximum width or height of these buttons. Therefore, it's not immediately surprising that they are too large. All this code says is, "the bottoms of all these buttons must be flush with the bottom of the superview," and "these buttons must be arranged horizontally touching in a given order, with equal widths." Note that `UIButton` has a built in `intrinsicContentSize`, which you have to take into consideration. – ravron Nov 08 '14 at 23:52
  • You commented on my answer to this question, http://stackoverflow.com/questions/26268387/auto-layout-equal-space-between-uibuttons/26268470#26268470. That's the way you need to do it, using spacer views. I don't know why it didn't work for you then since it works fine when I test it. – rdelmar Nov 09 '14 at 00:48
  • @Riley, my intend is to set up all buttons frame to 20x20; 'intrinsicContentSize' is only when I subclass UIButton, right? – Idan Moshe Nov 09 '14 at 05:39
  • @rdelmar, I tried your code again and it's work, but still the size is huge, over 20x20. – Idan Moshe Nov 09 '14 at 05:39
  • @IdanMoshe, I thought you said you wanted a maximum size of 40x40, so what's wrong with 20x20? That's actually smaller than Apple's recommended size for a control. – rdelmar Nov 09 '14 at 05:46
  • OK, my bad,40x40 is my goal, not 20x20. But it doesn't matter, since I'm struggling how to do it. – Idan Moshe Nov 09 '14 at 06:22
  • @IdanMoshe, even if you do not subclass `UIButton`, the buttons still have their own, Apple-defined `intrinsicContentSize`. I'm simply noting that this value may add constraints which you are not expecting. To determine it, try calling `intrinsicContentSize` on one of your buttons after you have set it up, and examine the `CGSize` struct returned. – ravron Nov 09 '14 at 17:02

0 Answers0