ios - UIPageControl 点边框颜色

标签 ios objective-c

我无法在 iOS 14 中设置页面控制点的边框颜色。以下代码适用于 iOS < 14,但不适用于 iOS 14:

for (int i = 0; i < [self.subViews count]; i++) {
    UIView *dot = [self.subViews objectAtIndex:i];
    dot.layer.borderWidth = 1;
    dot.layer.borderColor = [UIColor blueColor].CGColor;
}
UIPageControl 色调颜色清晰。我只想添加边框。在 Obj-C 中需要一个解决方案。

最佳答案

评论

  • iOS 14 仍处于测试阶段,任何基于 View 层次结构的解决方案都可能停止使用下一个测试版本。
  • 它还可以在任何 future 的 iOS 版本(非测试版)中停止工作。
  • 您依赖于私有(private) View 层次结构。 Apple 可以随时更改它,无需另行通知。 IOW 它属于私有(private) API 使用类别 = 你自己在这里。

  • UIPageControl View 层次结构
    iOS 13
    enter image description here
    iOS 14
    enter image description here
    新 API
    即使您更新代码以匹配新的 View 层次结构......
    UIView *pageControlContentView = self.pageControl.subviews[0];
    UIView *pageControlIndicatorContentView = pageControlContentView.subviews[0];
    [pageControlIndicatorContentView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.layer.borderWidth = 1;
        obj.layer.borderColor = UIColor.blueColor.CGColor;
    }];
    
    ...你会得到这样的东西:
    enter image description here
    iOS 14 引入了可以使用的新属性和方法:
  • preferredIndicatorImage
  • setIndicatorImage(_:forPage:)
  • indicatorImageForPage:

  • 您可以使用 SF 符号,例如:
    UIImage *image = [UIImage systemImageNamed:@"link.circle.fill"];
    self.pageControl.preferredIndicatorImage = image;
    
    然后你会得到:
    enter image description here
    换句话说,为了安全起见,避免私有(private) View 层次结构,使用 preferredIndicatorImage .如果您想自定义图像等,您可以准备自己的图像。但这是另一个问题的主题。如果您想以编程方式进行,请搜索问题 like this one .
    由于您使用的是私有(private) View 层次结构,因此请学习您的工具(Xcode)并从 Examining the View Hierarchy 开始。和 Debugging View Hierarchies章节。下次您将能够找到自己的问题所在。

    关于ios - UIPageControl 点边框颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63289401/

    相关文章:

    ios - 在 SpriteKit 中重置场景

    objective-c - 将 NSmutableArray 保存到文件并加载

    ios - 自定义 UIAlertView 和操作表

    iphone - 如何在 OpenGL ES 中创建广告牌?

    ios - UIActivityViewController 包括而不是排除事件类型

    ios - 对于 iOS,Concurrency Programming Guide 和 Threading Programming Guide 有什么区别?

    ios - 什么时候使用 NSSet 而不是 NSArray 更好?

    ios - 带有未在后台调用的请求 block 的 URLSession.datatask

    android - 使用 ionic 推送通知的自定义声音

    objective-c - Objective C self.variable 和变量赋值之间的区别