Frage

EDIT

I am using programmatic auto layout and this issue seems to be eluding me,

in this class

@interface FooterButtonView : UIView {
...
}

I am trying to line up two views side by side

    - (void)setUpViewWithTwoElements:(UIView*)element1 :(UIView*)element2{

    element1.translatesAutoresizingMaskIntoConstraints = NO;
    element2.translatesAutoresizingMaskIntoConstraints = NO;

    NSDictionary* views = @{@"element1":element1, @"element2":element2};
    NSDictionary* metrics = @{@"buttonHeight":@30.0};
    NSString* horizontalFormatString = @"H:|-[element1]-[element2]-|";
    NSString* verticalFormatString = @"V:[element1(buttonHeight)]-|";

    [self addConstraints:[NSLayoutConstraint
                            constraintsWithVisualFormat:horizontalFormatString
                            options: NSLayoutFormatAlignAllTop | NSLayoutFormatAlignAllBottom
                            metrics:nil
                            views:views]];

    [self addConstraints:[NSLayoutConstraint
                          constraintsWithVisualFormat:verticalFormatString
                          options:nil
                          metrics:metrics
                          views:views]];
}

however neither elements is being displayed.

in init I am adding both subviews and then calling the above function. Both elements descend from UIButton.

Any suggestions are greatly appreciated. Thank you!

War es hilfreich?

Lösung

You should see something with the code you posted assuming that the init method that calls this code is itself being called (and FooterButtonView is being displayed with a non-zero size). One thing you're missing though is the relative horizontal sizes of the two views. With the code you have, there's no way for the system to know what size each of the elements should be, just that they should take up the whole width minus the standard spacings. If you want the two views to be the same size, then change to this,

NSString* horizontalFormatString = @"H:|-[element1]-[element2(==element1)]-|";
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top