-1

I'm messing around with a really simple app to learn how to use AVFoundation (only been coding at all for about 14 weeks).

Included is a screenshot to help visualize my problem - my vertical constraints work just fine, and my horizontal constraints appear for two buttons that I have. However, my horizontal constraints (which I'm using to center a few objects) do not seem to work for the two labels I have underneath each button.

I wonder if the problem is that some constraints (perhaps the way I've created them) take priority over others and prevent some constraints from appearing properly? Really not sure here.

enter image description here

-(void)setConstraints {
    [self.view removeConstraints:self.view.constraints];

    UIButton *cameraButton = self.cameraButton;
    UILabel *camera = self.videoLabel;
    UIButton *libraryButton = self.libraryButton;
    UILabel *library = self.libraryLabel;

    NSDictionary *views = NSDictionaryOfVariableBindings(camera, cameraButton, libraryButton, library);

    //set up top button to be horizontally centered
    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-[cameraButton]-|"
                                                                   options:0
                                                                   metrics:nil
                                                                     views:views];
    //set up top button vertical from top of superview
    constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat: @"V:|-175-[cameraButton]"
                                                                                                     options:0
                                                                                                     metrics:nil
                                                                                                    views:views]];
    //set up top button label to be horizontally centered
    constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat: @"|-[camera]-|"
                                                                                                     options:0
                                                                                                     metrics:nil
                                                                                                       views:views]];

    //set up second button to be horizontally centered
    constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat: @"|-[libraryButton]-|"
                                                                                                     options:0
                                                                                                     metrics:nil
                                                                                                       views:views]];
    //set up label for second button to be horizontally centered
    constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat: @"|-[library]-|"
                                                                                                     options:0
                                                                                                     metrics:nil
                                                                                                       views:views]];

    //set up vertical constraints by spacing ALL objects appropriately
    constraints = [constraints arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat: @"V:[cameraButton]-[camera]-150-[libraryButton]-[library]"
                                                                                                     options:0
                                                                                                     metrics:nil
                                                                                                       views:views]];

    [self.view addConstraints:constraints];

}
Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
karan satia
  • 307
  • 4
  • 16
  • "horizontal constraints do not seem to work" The phrase "do not seem to work" is meaningless. Say clearly what you expect / want that is different from what is happening. – matt Aug 20 '15 at 20:04
  • @matt edited. If you'd read past the first few sentences and into the code just a little, you'd see that my comments clearly outline that I'm trying to center everything. – karan satia Aug 20 '15 at 20:21
  • Well, as you've already been told, your labels _are_ centered. The problem is that the _text_ is not centered within the labels. But that has nothing to do with constraints! – matt Aug 20 '15 at 20:23
  • Yep. Today was my first day really looking at it, so I'm still getting the hang of it. Powerful tool...btw, love your books @matt – karan satia Aug 20 '15 at 20:33
  • Thanks, glad if they're helpful. – matt Aug 20 '15 at 22:29

1 Answers1

0

My guess is you wanted these views to appear centered. But that isn't what a "|-[view]-|" constraint will do. What you just told the view is, "I want you to fill the entire width of your superview, modulo the default padding". If you were to set a background color on your labels you'd see them extending the entire width of the view.

The easiest solution for you here is to set the text layout to center.

Joshua Weinberg
  • 28,598
  • 2
  • 97
  • 90
  • Makes sense. I'll try it. I may also just use spacing items. Wish I could post the code in a comment but it's way too messy. It would just involve creating two blank UIViews that have nothing in them and doing something along the lines of: @"H:|[spacer1][label][spacer2(==spacer1)]|" This seems, however, like it's too much to have to do for each item that I want centered. – karan satia Aug 20 '15 at 20:24