在UIPageControl中添加点的边框

Emr*_*gül 16 uipagecontrol ios

我想在UIPageControl中为点添加边框颜色.这是它的小图:

在此输入图像描述

我可以通过从XCode配置它来放置第二个点,但我不能让第一个和第三个圆圈在里面空.有没有一种简单的方法来实现这一目标?

谢谢 :)

Roh*_*wal 21

编辑 - Swift 3&4扩展以实现相同的结果 -

extension UIPageControl {

    func customPageControl(dotFillColor:UIColor, dotBorderColor:UIColor, dotBorderWidth:CGFloat) {
        for (pageIndex, dotView) in self.subviews.enumerated() {
            if self.currentPage == pageIndex {
                dotView.backgroundColor = dotFillColor
                dotView.layer.cornerRadius = dotView.frame.size.height / 2
            }else{
                dotView.backgroundColor = .clear
                dotView.layer.cornerRadius = dotView.frame.size.height / 2
                dotView.layer.borderColor = dotBorderColor.cgColor
                dotView.layer.borderWidth = dotBorderWidth
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

使用它在viewDidLoad()或viewDidAppear()中写下面的代码

pageControl.customPageControl(dotFillColor: .orange, dotBorderColor: .green, dotBorderWidth: 2)
Run Code Online (Sandbox Code Playgroud)

在Objective-C中使用以下代码 -

- (void) customPageControlWithFillColor:(UIColor*)dotFillColor borderColor:(UIColor*)dotBorderColor borderWidth:(CGFloat)dotBorderWidth {
    for (int pageIndex = 0; pageIndex < _pageControl.numberOfPages; pageIndex++) {
        UIView* dotView = [_pageControl.subviews objectAtIndex:pageIndex];
        if (_pageControl.currentPage == pageIndex) {
            dotView.backgroundColor = dotFillColor;
            dotView.layer.cornerRadius = dotView.frame.size.height / 2;
        } else {
            dotView.backgroundColor = [UIColor clearColor];
            dotView.layer.cornerRadius = dotView.frame.size.height / 2;
            dotView.layer.borderColor = dotBorderColor.CGColor;
            dotView.layer.borderWidth = dotBorderWidth;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

输出 -

在此输入图像描述

  • @EmrahAkgül如果你在storyboard中设置页面控件,那么在`viewDidAppear`之后尝试调用`for`循环.它对我有用.当我在`viewDidLoad`中调用循环时,它无法正常工作.谢谢@RiosK (3认同)
  • `borderColor` 和 `borderWidth` 设置就足够了。剩下的就不需要了。状态的“backgroundColor”可以使用“pageIndicatorTintColor”和“currentPageIndicatorTintColor”设置。赞成这一点,因为它有帮助。确保在视图出现后调用,如上面提到的@TheRohanSanap。 (2认同)

Luk*_*ers 19

另一种方法是使用正确尺寸的图案图像(目前直径为7个点).结果如下:

分页点截图

以下是它的完成方式:

let image = UIImage.outlinedEllipse(size: CGSize(width: 7.0, height: 7.0), color: .darkGray)
self.pageControl.pageIndicatorTintColor = UIColor.init(patternImage: image!)
self.pageControl.currentPageIndicatorTintColor = .darkGray
Run Code Online (Sandbox Code Playgroud)

使用这个简单的小扩展UIImage:

/// An extension to `UIImage` for creating images with shapes.
extension UIImage {

    /// Creates a circular outline image.
    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)
        // Inset the rect to account for the fact that strokes are
        // centred on the bounds of the shape.
        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
    }
}
Run Code Online (Sandbox Code Playgroud)

这样做的缺点是,如果操作系统更新中的点大小发生变化,则图像看起来会很奇怪,因为它会被平铺或剪裁.


Jer*_*ome 15

来自@RiosK的SWIFT 3版本

func updatePageControl() {
    for (index, dot) in pageControl.subviews.enumerated() {
        if index == pageControl.currentPage {
            dot.backgroundColor = dotColor
            dot.layer.cornerRadius = dot.frame.size.height / 2;
        } else {
            dot.backgroundColor = UIColor.clear
            dot.layer.cornerRadius = dot.frame.size.height / 2
            dot.layer.borderColor = dotColor.cgColor
            dot.layer.borderWidth = dotBorderWidth
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我必须将调用好函数放入viewDidAppear和didFinishAnimating中才能使其正常工作。 (2认同)

小智 8

斯威夫特 4。您可以指定 borderColor 然后观察 currentPage 属性来更改点的边框:

class CustomPageControl: UIPageControl {

    var borderColor: UIColor = .clear

    override var currentPage: Int {
        didSet {
            updateBorderColor()
        }
    }

    func updateBorderColor() {
        subviews.enumerated().forEach { index, subview in
            if index != currentPage {
                subview.layer.borderColor = borderColor.cgColor
                subview.layer.borderWidth = 1
            } else {
                subview.layer.borderWidth = 0
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)


Hem*_*ang 6

对于当前可用的属性,这是不可能的UIPageControl.但您可以通过集成任何模仿iOS功能的第三方页面控件来实现UIPageControl.

其他答案已应用补丁.我非常不同意这个解决方案.