[iOS]给UIPageControl设置圆点间距

2017-03-07  本文已影响1017人  溜萝卜

** 平常用pageControl用的少,今天发现怎么setFrame,setSize,setBounds都对间距不起作用,所以跟大家分享一下在网上找到的解决办法**

#define dotW 4.5
#define activeDotW 6
#define margin 5
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //计算圆点间距
    CGFloat marginX = margin + 5;
    
    //计算整个pageControll的宽度
    CGFloat newW = (self.subviews.count - 1 ) * marginX;
    
    //设置新frame
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, newW, self.frame.size.height);
    
    //设置居中
    CGPoint center = self.center;
    center.x = self.superview.center.x;
    self.center = center;
    
    //遍历subview,设置圆点frame
    for (int i=0; i<[self.subviews count]; i++) {
        UIImageView* dot = [self.subviews objectAtIndex:i];
        
        [dot setFrame:CGRectMake(i * marginX, dot.frame.origin.y, dotW, dotW)];
        
    }
}
 [pagecontrol setValue:[UIImage imageNamed:@"icon_xuanzhong"] forKeyPath:@"_currentPageImage"];
 [pagecontrol setValue:[UIImage imageNamed:@"icon_weixuanzhong"] forKeyPath:@"_pageImage"];
上一篇下一篇

猜你喜欢

热点阅读