Search code examples
iphoneobjective-ciosuipagecontrol

UIPageControl subclass override


I am attempting to make a left aligned UIPageControl (rather than centered). I see that subclasses can override - (CGSize)sizeForNumberOfPages:(NSInteger)pageCount, but it does not seem to be getting called. Below is what my subclass looks like:

@implementation CustomPageControl

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (CGSize)sizeForNumberOfPages:(NSInteger)pageCount {
    CGSize size = self.bounds.size;
    NSLog(@"%f, %f", size.width, size.height);
    return size;
}

@end

I am even explicitly trying to call [pageControl sizeToFit] , [pageControl setNeedsLayout] , and [pageControl setNeedsDisplay]. Never being called.

Any idea on why that is not being called, if that is the right method to override, or if there are an examples on how to solve this? Thanks.


Solution

  • Actually, I figured I can just use - (CGSize)sizeThatFits:(CGSize)size but then realized it wasn't needed.

    Just made the frame of the page control smaller depending on number of pages.

    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, y, 16*([photos count]+1), width)];