【发布时间】:2019-11-01 00:24:06
【问题描述】:
我有一个视图控制器,在它上面我有段控制,我在滑动手势上切换段,现在我希望当我切换段时,当前段标题颜色应该变成白色,剩余颜色应该变成灰色,我已经搜索过了但是我得到了背景颜色更改的结果,如何在段之间切换时更改段控制标题颜色?这是我的滑动段代码,
let titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
segmentControl.setTitleTextAttributes(titleTextAttributes, for: .selected)
segmentControl.fallBackToPreIOS13Layout(using: UIColor.clear)
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.swipedRight))
swipeRight.direction = UISwipeGestureRecognizer.Direction.right
self.activeView.addGestureRecognizer(swipeRight)
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.swipedLeft))
swipeLeft.direction = UISwipeGestureRecognizer.Direction.left
self.closedView.addGestureRecognizer(swipeLeft)
@objc func swipedRight(){
segmentControl.selectedSegmentIndex = 0
self.activeView.isHidden = false
self.closedView.isHidden = true
let titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
segmentControl.setTitleTextAttributes(titleTextAttributes, for: .selected)
let titleText = [NSAttributedString.Key.foregroundColor: UIColor.gray]
segmentControl.setTitleTextAttributes(titleText, for: .disabled)
getActiveQuestionAPI()
}
@objc func swipedLeft(){
segmentControl.selectedSegmentIndex = 1
self.activeView.isHidden = true
self.closedView.isHidden = false
let titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
segmentControl.setTitleTextAttributes(titleTextAttributes, for: .selected)
let titleText = [NSAttributedString.Key.foregroundColor: UIColor.gray]
segmentControl.setTitleTextAttributes(titleText, for: .disabled)
getCloseQuestionAPI()
}
【问题讨论】:
标签: ios swift uisegmentedcontrol