ios - 在 UIPageControl 中为点添加边框

标签 ios uipagecontrol

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

enter image description here

我可以通过从 XCode 配置它来放置第二个点,但我不能使第一个和第三个圆圈的内部为空。有没有一种简单的方法可以实现这一点?

谢谢:)

最佳答案

已编辑 - 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
            }
        }
    }

}

要使用它,请在 viewDidLoad() 或 viewDidAppear() 中编写以下代码

pageControl.customPageControl(dotFillColor: .orange, dotBorderColor: .green, dotBorderWidth: 2)

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;
        }
    }
}

输出-

enter image description here

关于ios - 在 UIPageControl 中为点添加边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35842040/

相关文章:

ios - 如何在 IOS 中使用 AVFoundation 框架设置相机的缩放级别?

ios - 如何在iOS模拟器上将应用启动到后台?

ios - UIPageControl View subview 中为 nil

ios - 设置后设置 View Controller 时以编程方式添加的页面控件消失

ios - 使用页面控件 ScrollView 以在 iOS 中显示图像

ios - 为不是 TabBar 项目但在菜单中的项目显示主 TabBar(使用 SWRevealCotroller)

iphone - iOS 错误选择器错误

ios - 拖动多个图像

ios - 检测 UIPageControl 页面变化

ios - 页面 Controller 宽度 (iPhone)