3

I want to horizontally center a number of UIViews (they happen to be circles) in the master UIView. It will end up basically looking like the dots on the standard Page Control.

I have all the code written to create the circle UIViews I just have no idea how to arrange them horizontally and dynamically at run time.

Essentially I need some kind of horizontal container where I can do this

-(void)addCircle{
  [self addSubView:[CircleView init]];
}

And it will auto arrange however many children it has in the center.

Chris
  • 26,744
  • 48
  • 193
  • 345
  • Tried `layoutSubviews` or auto-layout constraints? – Wain Oct 29 '13 at 22:29
  • Which are you using, auto layout or autoresizing masks? – Lance Oct 29 '13 at 22:29
  • I have constraints enabled but I find them extremely counter intuitive compared to, well, ever other technology I've used lol. I was hoping someone could advise the best way to do it as I literally have no idea where to start... – Chris Oct 29 '13 at 22:40

3 Answers3

11

I get confused with auto-layout as well from time to time but here is a way how you can do it programmatically: (I assume that you add your circle views to a containerView property of your view controller and you do not add any other views to it.)

  1. Add these two properties to your view controller:

    @property (nonatomic) CGRect circleViewFrame;
    @property (nonatomic) CGFloat delta;
    
  2. Initiate those properties with the desired values in your view controller's viewDidLoad method:

    // the size (frame) of your circle views
    self.circleViewFrame = CGRectMake(0, 0, 10, 10);
    // the horizontal distance between your circle views
    self.delta = 10.0;
    
  3. Now we add your "automatic addCircle method":

    - (void)addCircleView {
      UIView *newCircleView = [self createCircleView];
      [self.containerView addSubview:newCircleView];
      [self alignCircleViews];
    }
    
  4. Of course we need to implement the createCircleView method...

    - (UIView*)createCircleView {
      // Create your circle view here - I use a simple square view as an example
      UIView *circleView = [[UIView alloc] initWithFrame:self.circleViewFrame];
      // Set the backgroundColor to some solid color so you can see the view :)
      circleView.backgroundColor = [UIColor redColor];
    
      return circleView;
    }
    
  5. ... and the alignCircleViews method:

    - (void)alignCircleViews {
      int numberOfSubviews = [self.containerView.subviews count];
      CGFloat totalWidth = (numberOfSubviews * self.circleViewFrame.size.width) + (numberOfSubviews - 1) * self.delta;
      CGFloat x = (self.containerView.frame.size.width / 2) - (totalWidth / 2);
    
      for (int i = 0; i < numberOfSubviews; i++) {
          UIView *circleView = self.containerView.subviews[i];
          circleView.frame = CGRectMake(x,
                                  self.circleViewFrame.origin.y,
                                  self.circleViewFrame.size.width,
                                  self.circleViewFrame.size.height);
          x += self.circleViewFrame.size.width + self.delta;
      }
    }
    

This is the most important method which will automatically realign all your subviews each time a new circleView is added. The result will look like this:

sample view controller with 3 horizontally centered subviews sample view controller with 8 horizontally centered subviews

Mischa
  • 15,816
  • 8
  • 59
  • 117
0

Simple steps: append circle to container view, resize container view, center align container view

-(void)addToContanerView:(CircleView*)circle{

    circle.rect.frame = CGrectMake(containers_end,container_y,no_change,no_change);
    [containerView addSubview:circle];
    [containerView sizeToFit];
    containerView.center = self.view.center;
}

Assumptions: containers_end & containers_y you can get from CGRectMax function, for UIView SizeToFit method check here

To take care of rotation use make sure your Autoresizing subviews are set for left, right bottom and top margin.

Community
  • 1
  • 1
Kunal Balani
  • 4,739
  • 4
  • 36
  • 73
0

You can try using this library. I have used it on several of my projects and so far, it worked really well.

https://github.com/davamale/DMHorizontalView

dmlebron
  • 861
  • 6
  • 16