2016-02-26 61 views
1

更改颜色非常简单,但可以更改所有未选中点的边框吗?如何更改UIPageControl点的边界?

例:

dot.layer.borderWidth = 0.5 dot.layer.borderColor = UIColor.blackColor()

+0

我不认为这是可能的。但是你可以轻松地实现你自己的页面控件,可以做到这一点。 – dasdom

回答

2

是可以这样做.. 其实它很简单。

Pagecontrol由许多可以访问的子视图组成。 self.pageControl.subviews返回你的[UIView]即UIView的数组。 获得单个视图后,您可以为其添加边框,更改其borderColor,更改其边框宽度,像缩放它一样转换点大小。可以使用UIView所具有的所有属性。

   for index in 0..<array.count{ // your array.count 

       let viewDot = weakSelf?.pageControl.subviews[index] 
       viewDot?.layer.borderWidth = 0.5 
       viewDot?.transform = CGAffineTransform(scaleX: 1.2, y: 1.2) 

       if (index == indexPath.row){ // indexPath is the current indexPath of your selected cell or view in the collectionView i.e which needs to be highlighted 
        viewDot?.backgroundColor = UIColor.black 
        viewDot?.layer.borderColor = UIColor.black.cgColor 
       } 
       else{ 
        viewDot?.backgroundColor = UIColor.white 
        viewDot?.layer.borderColor = UIColor.black.cgColor 

       } 
      } 

,它看起来像这样

enter image description here

记住你不需要设置weakSelf?.pageControl.currentPage = indexPath.row。做让我知道在任何情况下,问题..希望这可以解决你的问题。 所有最优秀的

+0

嗨!我测试了你的代码(但在客观的c),我无法获得边界。你能帮我吗? –

+0

当然。你在哪个方法使用这个代码? –

+0

我在viewDidLoad中使用了这段代码 –

0

扩展集的PageControl指标边境/斯威夫特3

extension UIImage { 
    class func outlinedEllipse(size: CGSize, color: UIColor, lineWidth: CGFloat = 1.0) -> UIImage? { 
     UIGraphicsBeginImageContextWithOptions(size, false, 0.0) 
     guard let context = UIGraphicsGetCurrentContext() else { 
      return nil 
     } 

     context.setStrokeColor(color.cgColor) 
     context.setLineWidth(lineWidth) 
     let rect = CGRect(origin: .zero, size: size).insetBy(dx: lineWidth * 0.5, dy: lineWidth * 0.5) 
     context.addEllipse(in: rect) 
     context.strokePath() 

     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return image 
    } 
    } 

用途:

let image = UIImage.outlinedEllipse(size: CGSize(width: 7.0, height: 7.0), color: .lightGray) 
self.pageControl.pageIndicatorTintColor = UIColor.init(patternImage: image!) 
self.pageControl.currentPageIndicatorTintColor = .lightGray