自定义UISegmented控件

时间:2017-11-02 10:46:57

标签: ios objective-c uisegmentedcontrol

我如何在iOS UISegmentedControl(Objective-C)

中实现此目的

我想显示所选分段控件的舍入选择。

enter image description here

任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:1)

UISegmentedControl有一个customization部分。

总结如下: 您所要做的就是为不同的状态组合提供分频器图像。这意味着您需要为以下状态提供3个图像:

  1. 两个未选定的片段之间的图像
  2. 左侧选择的段与右侧未选中的段之间的图像
  3. 在右侧选择的段和左侧未选中的段之间的图像
  4. 代码如下:

    <强>目标C

    // Image between two unselected segments.
    [mySegmentedControl setDividerImage:image1 forLeftSegmentState:UIControlStateNormal
                      rightSegmentState:UIControlStateNormal barMetrics:barMetrics];
    // Image between segment selected on the left and unselected on the right.
    [mySegmentedControl setDividerImage:image1 forLeftSegmentState:UIControlStateSelected
                      rightSegmentState:UIControlStateNormal barMetrics:barMetrics];
    // Image between segment selected on the right and unselected on the right.
    [mySegmentedControl setDividerImage:image1 forLeftSegmentState:UIControlStateNormal
                      rightSegmentState:UIControlStateSelected barMetrics:barMetrics];
    

    Swift

        // Image between two unselected segments.
        mySegmentedControl.setDividerImage(myImage, forLeftSegmentState: UIControlState.Normal,
                                           rightSegmentState: UIControlState.Normal, barMetrics: UIBarMetrics.Default)
    
        // Image between segment selected on the left and unselected on the right.
        mySegmentedControl.setDividerImage(myImage, forLeftSegmentState: UIControlState.Selected,
                                           rightSegmentState: UIControlState.Normal, barMetrics: UIBarMetrics.Default)
    
        // Image between segment selected on the right and unselected on the left.
        mySegmentedControl.setDividerImage(myImage, forLeftSegmentState: UIControlState.Normal,
                                           rightSegmentState: UIControlState.Selected, barMetrics: UIBarMetrics.Default)
    

    这是一个post,解释了如何实现这一目标。

    如果你看到链接的帖子那么是3张图片。附上截图供参考: enter image description here