Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatic Vertical Alignment of UIPageControl in Superview using NSLayoutConstraint

Up until now, I was able to avoid using Auto Layout, so apologies for the (potentially) stupid question.

  • I have a ViewController that is being built programmatically (e.g., no IB).
  • I have a UIPageControl object that is in the view hierarchy of the viewController's view (e.g., addSubview:)
  • I need, rather, want to align a UIPageControl object against the bottom edge of the UIViewController's main view.
  • I want the UIPageControl to be centered horizontally, but 20-40 pixels from the bottom's edge.

Here's some sample code I have that is creating the UIPageControl and displaying it, but not in the correct location at all.

self.pageControl = [[UIPageControl alloc] init];
self.pageControl.numberOfPages = ...;
self.pageControl.otherAttributes = ...;
[self.view addSubview:[self pageControl]];

UIPageControl *pageControl = [self pageControl];
UIView *view = [self view];
NSDictionary *constaintDictionary = NSDictionaryOfVariableBindings(view, pageControl);


[self.view addConstraint:[NSLayoutConstraint constraintWithItem:pageControl
                                                     attribute:NSLayoutAttributeCenterX
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:self.view
                                                     attribute:NSLayoutAttributeCenterX
                                                    multiplier:1
                                                      constant:0]];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[pageControl]-20-[view]"
                                                                  options:0
                                                                  metrics:nil
                                                                    views:constaintDictionary]];

Like I said, I'm really new to auto-layout, so I'm not sure if I'm even implementing these methods properly.

Thank you for your assistance ahead of time.

like image 606
ArtSabintsev Avatar asked Nov 06 '13 21:11

ArtSabintsev


1 Answers

Alright, I figured out how to do it. Took an inordinate amount fo time, but after i read through the Visual Format Language docs again, I came up with this:

UIPageControl *pageControl = [self pageControl];

[pageControl setTranslatesAutoresizingMaskIntoConstraints:NO];

[self.view addConstraints:[NSLayoutConstraint
                           constraintsWithVisualFormat:@"H:|-0-[pageControl]-0-|"
                           options:0
                           metrics:nil
                           views:NSDictionaryOfVariableBindings(pageControl)]];
[self.view addConstraints:[NSLayoutConstraint
                           constraintsWithVisualFormat:@"V:[pageControl]-40-|"
                           options:0
                           metrics:nil
                           views:NSDictionaryOfVariableBindings(pageControl)]];
like image 189
ArtSabintsev Avatar answered Nov 14 '22 23:11

ArtSabintsev