我有一个分段控件,它有4个分段,如图所示。
Foe ex:有线,有管理的机动性
我想在选择任何线段时在底部(发际线)显示一个红色;基本上它应该在所选线段处显示红线
我现在的代码如下

func changeColor(sender: UISegmentedControl){

    if sender.selectedSegmentIndex == 0 {
        UIView.animateWithDuration(0.5, animations: {
            self.containerAccountSummary.alpha = 1
            self.containerLocationTracker.alpha = 0
            self.containerWireline.alpha = 0
            self.containerManagedMobility.alpha = 0
            self.containerMobileVoiceAndData.alpha = 0
        })
    } else if sender.selectedSegmentIndex == 1{
        UIView.animateWithDuration(0.5, animations: {
            self.containerAccountSummary.alpha = 0
            self.containerLocationTracker.alpha = 1
            self.containerWireline.alpha = 0
            self.containerManagedMobility.alpha = 0
            self.containerMobileVoiceAndData.alpha = 0
        })
    }else if sender.selectedSegmentIndex == 2{
        UIView.animateWithDuration(0.5, animations: {
            self.containerAccountSummary.alpha = 0
            self.containerLocationTracker.alpha = 0
            self.containerWireline.alpha = 1
            self.containerManagedMobility.alpha = 0
            self.containerMobileVoiceAndData.alpha = 0
        })

    }else if sender.selectedSegmentIndex == 3{
        UIView.animateWithDuration(0.5, animations: {
            self.containerAccountSummary.alpha = 0
            self.containerLocationTracker.alpha = 0
            self.containerWireline.alpha = 0
            self.containerManagedMobility.alpha = 1
            self.containerMobileVoiceAndData.alpha = 0
        })

    }else {
        UIView.animateWithDuration(0.5, animations: {
            self.containerAccountSummary.alpha = 0
            self.containerLocationTracker.alpha = 0
            self.containerWireline.alpha = 0
            self.containerManagedMobility.alpha = 0
            self.containerMobileVoiceAndData.alpha = 1
        })
    }
}

有什么建议吗?

最佳答案

您可以将UIView设置为hairline并将其放在下面,以便在情节提要上分段控制。指定了颜色和高度。将其宽度设置为段控件中的单段宽度,然后在调用段控件操作时,获取选定的段索引,并根据该索引,通过提供X位置、Y位置、高度和宽度来更改UIView的帧。然后将此帧设置为hairline并在hairline上调用layoutIfNeeded()方法。它会改变发际线的框架。在下面编写一个函数,并在段控件的操作方法中调用此函数:

override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        let segmentControlFrame = self.view.convert(segmentControl.frame, from: segmentControl.superview)
        let widthForHairLine = segmentControlFrame.width / 4
        if segmentControl.selectedSegmentIndex == 0 {
            firstFrame = CGRect(x: segmentControlFrame.minX, y: segmentControlFrame.maxY , width: widthForHairLine, height: 5)
        } else if segmentControl.selectedSegmentIndex == 1 {
            firstFrame = CGRect(x: (segmentControlFrame.minX + widthForLine), y: segmentControlFrame.maxY , width: widthForHairLine, height: 5)
        } else if segmentControl.selectedSegmentIndex == 2 {
            firstFrame = CGRect(x: (segmentControlFrame.minX + widthForLine + widthForLine), y: segmentControlFrame.maxY , width: widthForHairLine, height: 5)
        } else if segmentControl.selectedSegmentIndex == 3 {
            firstFrame = CGRect(x: (segmentControlFrame.minX + widthForLine + widthForLine + widthForLine), y: segmentControlFrame.maxY , width: widthForHairLine, height: 5)
        self.hairline.backgroundColor = UIColor.red
        self.hairline.frame = firstFrame!
        self.hairline.layoutIfNeeded()
    }

10-06 13:10