自定义UIPageControl点在加载时放错了位置

时间:2013-12-30 18:05:37

标签: ios objective-c uipagecontrol

我对自定义UIPageControl有一种奇怪的行为。 我将其子类化,并在setCurrentPage方法中绘制自定义点。

  • 在iOS 6中,他们只是错过了。
  • 在iOS 7中,这些点是错误的。

只有在我更换幻灯片后才能正确定位iOS的两个点。

我尝试了以下方法但没有成功:

[customPageControl updateCurrentPageDisplay];
[customPageControl updateConstraints];
[customPageControl sizeToFit];
[customPageControl.currentPage = 1];

代码:

@implementation ProfilePagesPageControl

@synthesize originalSubviews;
@synthesize dotsHeight;

- (void)setCurrentPage:(NSInteger)page
{
    [super setCurrentPage:page];

    [self updateDots];
}

- (void)updateDots
{
    for (int i = 0; i < self.subviews.count; i++)
    {
        UIView* dotView = [self.subviews objectAtIndex:i];

        for (int j = dotView.subviews.count - 1; j >= 0; j--)
        {
            UIView *view = [dotView.subviews objectAtIndex:j];
            [view removeFromSuperview];
        }

        int widthForDots = [Utils getDeviceBounds].width - Constraints.Gap * 3;
        int effectiveWidth = widthForDots / self.subviews.count - Constraints.Gap;
        int effectiveGap = (widthForDots - (effectiveWidth * self.subviews.count) + Constraints.Gap * 3) / self.subviews.count;

         int effectiveHeight = dotsHeight;
        if (i == self.currentPage)
            effectiveHeight = effectiveHeight + 3;
         else
            effectiveHeight = effectiveHeight - 3;


        CGRect dotViewFrame = dotView.frame;
        dotViewFrame.origin.x = effectiveWidth * i + Constraints.Gap + effectiveGap * i;
        dotViewFrame.origin.y = self.frame.size.height / 2 - effectiveHeight / 2;
        dotViewFrame.size.width = effectiveWidth;
        dotViewFrame.size.height = effectiveHeight;
        dotView.frame = dotViewFrame;

        UIView *dotSubView = [[UIView alloc] initWithFrame:CGRectMake(0,
                                                                  0,
                                                                  dotView.frame.size.width,
                                                                  dotView.frame.size.height)];

        if (i == self.currentPage)
            dotSubView.backgroundColor = [UIColor blackColor];
        else
            dotSubView.backgroundColor = [UIColor grayColor];

        [UIUtils roundCorner:(UIRectCornerTopLeft | UIRectCornerTopRight | UIRectCornerBottomLeft | UIRectCornerBottomRight) forView:dotSubView];

        ProfileSlideView *slideView = [self.originalSubviews objectAtIndex:i];
        RevUILabel *label = [[RevUILabel alloc] initWithSize:13];
        label.text = slideView.name;

        label.frame = CGRectMake(dotSubView.frame.size.width / 2 - label.frame.size.width / 2,
                             dotSubView.frame.size.height / 2 - label.frame.size.height / 2,
                             label.frame.size.width,
                             label.frame.size.height);

        [dotSubView addSubview:label];
        [dotView addSubview:dotSubView];
    }
}
@end

iOS7外观:

The iOS7 look


iOS6外观:

The iOS6 look


幻灯片后看:

After slide look


1 个答案:

答案 0 :(得分:0)

确保在其他地方(例如viewDidLoad或viewWillAppear)调用updateDots,以便在切换任何页面之前设置它们。如果您只在updateDots中拨打setCurrentPage,则只有在您第一次切换页面时才会调用它。