【问题标题】:Custom UIPageControl dots are misplaced on load自定义 UIPageControl 点在加载时错位
【发布时间】:2013-12-30 18:05:36
【问题描述】:

我对自定义 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 外观:


iOS6 外观:

:


幻灯片查看后:


【问题讨论】:

  • 请贴出问题截图。
  • 你能发布你的代码来设置自定义点吗?
  • 我已经编辑了问题
  • 需要问题的截图。为了更好地理解..
  • 有没有在viewdidload中调用setCurrentPage方法,试试看。

标签: ios objective-c uipagecontrol


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-21
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    • 2015-04-09
    相关资源
    最近更新 更多