如何更改UIPageControl点的边界?

改变颜色是非常简单的,但是可以改变所有未被select的点的边界吗?

例如:

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

是的这可以做到..其实它非常简单。

Pagecontrol由许多可以访问的Subview组成。 self.pageControl.subviews返回你[UIView]即UIView的数组。 获得单一视图后,您可以添加边框,更改边框颜色,更改边框宽度,像缩放它一样转换点的大小。可以使用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 ie 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 } } 

看起来像这样

在这里输入图像说明

并记住你不需要设置weakSelf?.pageControl.currentPage = indexPath.row 。请让我知道万一有任何问题..希望这可以解决您的问题。 祝一切顺利

扩展设置页面控制指示符边界/ Swift 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