【问题标题】:UIPageControl dot border colorUIPageControl 点边框颜色
【发布时间】:2020-08-06 18:08:38
【问题描述】:

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

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 objective-c


    【解决方案1】:

    评论

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

    UIPageControl 视图层次结构

    iOS 13

    iOS 14

    新 API

    即使您更新代码以匹配新的视图层次结构...

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

    ...你会得到这样的东西:

    iOS 14 引入了您可以使用的新属性和方法:

    您可以使用 SF Symbols 例如:

    UIImage *image = [UIImage systemImageNamed:@"link.circle.fill"];
    self.pageControl.preferredIndicatorImage = image;
    

    然后你会得到:

    换句话说,为了安全起见,避免私有视图层次结构,使用preferredIndicatorImage。如果您想自定义图像等,您可以准备自己的图像。但这是另一个问题的主题。如果您想以编程方式进行,请搜索问题like this one

    由于您使用的是私有视图层次结构,因此请学习您的工具 (Xcode) 并从 Examining the View HierarchyDebugging View Hierarchies 章节开始。下次你会发现自己有什么问题。

    【讨论】:

      【解决方案2】:

      嗯,我只发现这个像“解决方案”

      之前(

      之后(iOS 14):

      if #available(iOS 14.0, *) {
                  pageControl.currentPageIndicatorTintColor = .blue // custom color
                  pageControl.pageIndicatorTintColor = .blue.withAlphaComponent(0.3)
              } else {
                  // Fallback on earlier versions
                  for index: Int in 0...3 {
                      guard pageControl.subviews.count > index else { return }
                      let dot: UIView = pageControl.subviews[index]
                      dot.layer.cornerRadius = dot.frame.size.height / 2
                      if index == pageControl.currentPage {
                          dot.backgroundColor = .blue                        
                          dot.layer.borderWidth = 0
                      } else {
                          dot.backgroundColor = UIColor.clear
                          dot.layer.borderColor = UIColor.blue.cgColor
                          dot.layer.borderWidth = 1
                      }
                  }
      }
      

      【讨论】:

        【解决方案3】:

        iOS 14 允许使用 SFSymbol 设置指示器图像,因此您可以使用 circle.fill 和简单的圆形符号作为点。

        func updateBorderColor() {
        if #available(iOS 14.0, *) {
            let smallConfiguration = UIImage.SymbolConfiguration(pointSize: 8.0, weight: .bold)
            let circleFill = UIImage(systemName: "circle.fill", withConfiguration: smallConfiguration)
            let circle = UIImage(systemName: "circle", withConfiguration: smallConfiguration)
            for index in 0..<numberOfPages {
                if index == currentPage {
                    setIndicatorImage(circleFill, forPage: index)
                } else {
                    setIndicatorImage(circle, forPage: index)
                }
            }
            pageIndicatorTintColor = selectionColor
            } else {
            subviews.enumerated().forEach { index, subview in
                if index != currentPage {
                    subview.layer.borderColor = selectionColor.cgColor
                    subview.layer.borderWidth = 1
                } else {
                    subview.layer.borderWidth = 0
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-08-25
          • 1970-01-01
          • 2011-02-25
          • 2012-10-01
          • 1970-01-01
          • 2010-12-24
          • 1970-01-01
          相关资源
          最近更新 更多