2017-09-20 2 views
1

UISegmetedControl에서 선택한 색을 제거하고 싶습니다. 나는 tintColor이 이것을 할 수 있지만 그것도 그걸로 글꼴 색상을 제거 알아요. 또한 kCTForegroundColorAttributeName을 사용하면 둘 다 제거됩니다.틴트 색을 제거하지만 글꼴 색 유지 UISegmentedControl

사이드 노트 UIView을 만들어 선택한 세그먼트 위에 배치하여 선택한 상태를 표시합니다. 나는 이것이 더 좋아 보일 것이라고 생각했다. 분기하고 내 자신의 사용자 지정 컨트롤을 만들려고합니다.

public let topLine = UIView() 

override func awakeFromNib() { 
    super.awakeFromNib() 
    self.removeBorders() 
    setFont() 
    addTopLine() 
} 

func setFont() { 
    let font = UIFont(name: FontTypes.avenirNextUltraLight, size: 22.0)! 
    let textColor = UIColor.MyColors.flatWhite 
    let attribute = [kCTFontAttributeName:font] 
    self.setTitleTextAttributes(attribute, for: .normal) 
} 

func addTopLine() { 
    topLine.backgroundColor = UIColor.MyColors.flatWhite 
    let frame = CGRect(x: 7, 
         y: -5, 
         width: Int(self.frame.size.width)/2, 
         height: 2) 
    topLine.frame = frame 
    self.addSubview(topLine) 
} 

struct FontTypes { 
    static let avenirNextRegular = "AvenirNext-Regular" 
    static let avenirLight = "Avenir-Light" 
    static let avenirNextUltraLight = "AvenirNext-UltraLight" 
} 

답변

2

하려면 tintColor는 선택된 세그먼트의

  • 배경 색상, 선택하지 않은 세그먼트의

  • 텍스트 색상과 UISegmentedControl의

  • 테두리 색상으로 첨부합니다.

그래서 tintColor를 흰색으로 변경하면 배경색과 색조 색이 모두 사라집니다.

mySegment.tintColor = .white 

let selectedAtrribute = [NSAttributedStringKey.foregroundColor: UIColor.red, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16)] 
mySegment.setTitleTextAttributes(selectedAtrribute as [NSObject : AnyObject], for: UIControlState.selected) 

let unselected = [NSAttributedStringKey.foregroundColor: UIColor.black, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16)] 
mySegment.setTitleTextAttributes(unselected as [NSObject : AnyObject], for: UIControlState.normal) 
:

당신은/선택하지 않은 텍스트 속성 아래와 같이 선정 설정해야