我有一个分段控件,它有4个段,如图所示。
敌人:有线,管理移动
当我选择任何片段时,我想在底部(发际线)获得红色;基本上它应该在选定的段显示红线
我目前的代码如下
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
})
}
}
任何建议?
答案 0 :(得分:0)
您可以将UIView用于发际线并将其放置在故事板上的分段控件下方。指定它的颜色和高度。在段控件中将其宽度等于sigle段,然后在调用段控制操作时,采用选定的段索引并根据该索引,通过提供X位置,Y位置,高度来更改UIView的帧和宽度。然后将此框架设置为发际线并在发际线上调用方法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()
}