【问题标题】:Custom Segmented Control in Swift for iOSSwift for iOS 中的自定义分段控件
【发布时间】:2017-08-26 20:26:10
【问题描述】:

我正在尝试实现分段控件,但它在布局中似乎很简单。我想自定义它:

  • 例如失去边界
  • 有一个自定义指标

有什么方法可以做到这一点?我知道在 Android 中如何自定义 TabLayout,但老实说我迷路了。

【问题讨论】:

    标签: ios swift uisegmentedcontrol


    【解决方案1】:

    我迟到了,但我会这样做:

    将背景颜色设置为.clear

        segmentedControlInstance.backgroundColor = UIColor(red:0.13, green:0.16, blue:0.29, alpha:1.0)
    

    将背景色调设置为 .clear

        segmentedControlInstance.tintColor = .clear
    

    我注意到所选片段的标题是粗体的。设置两种状态的文本属性(.normal & .selected)

        segmentedControlInstance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16)], for: .normal)
        segmentedControlInstance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 16)], for: .selected)
    

    最后,设置两个背景图片。注意,我不知道为 barMetrics 参数设置什么:

         segmentedControlInstance.setBackgroundImage(UIImage(name: "selectedSegment", for: .selected, barMetrics: ?)
         segmentedControlInstance.setBackgroundImage(UIImage(name: "normalSegment", for: .normal, barMetrics: ?)
    

    我会让你玩弄 barMetrics 参数。

    【讨论】:

      【解决方案2】:

      如果我需要自定义的内容不在此列表中,UISegmentedControl Docs,我会自己构建。

      在您的图片中,我将使用两个按钮、一个用于指示器的视图以及一些响应用户交互的逻辑。

      【讨论】:

        猜你喜欢
        • 2020-02-17
        • 1970-01-01
        • 1970-01-01
        • 2012-10-07
        • 1970-01-01
        • 2022-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多