MCNavBarSegmentView.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // MCNavBarSegmentView.swift
  3. // HCQuanfangtong
  4. //
  5. // Created by Apple on 2022/8/2.
  6. // Copyright © 2022 Jyp. All rights reserved.
  7. //
  8. import UIKit
  9. class MCNavBarSegmentView: MCNavBarView {
  10. init(dataSource: [String], tap: ((Int)->Void)?, style: MCNavBarStyle? = nil, defaultSelectedIndex: Int = 0){
  11. super.init(title: nil, subTitle: nil, style: style)
  12. self.segment = UISegmentedControl.init(items: dataSource)
  13. self.segment.selectedSegmentIndex = defaultSelectedIndex
  14. self.segment.tintColor = self.navStyle.styleForBackgroundColor
  15. self.segment.backgroundColor = self.navStyle.styleForTitleColor
  16. self.segment.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: self.navStyle.styleForTitleColor], for: .selected)
  17. // self.segment.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: self.navStyle.styleForBackgroundColor], for: .normal)
  18. // self.segment.setBackgroundImage(UIImage.init(color: self.navStyle.styleForBackgroundColor).stretchableImage(withLeftCapWidth: 0, topCapHeight: 0), for: .selected, barMetrics: .default)
  19. // self.segment.setBackgroundImage(UIImage.init(color: self.navStyle.styleForTitleColor).stretchableImage(withLeftCapWidth: 0, topCapHeight: 0), for: .normal, barMetrics: .default)
  20. _ = self.segment.rx.value.takeUntil(self.rx.deallocated).subscribe(onNext:tap)
  21. self.contentView.addArrangedSubview(self.segment)
  22. self.segment.mas_makeConstraints { make in
  23. make?.height.offset()(30)
  24. }
  25. self.segment.layer.borderWidth = 1
  26. self.segment.layer.borderColor = self.navStyle.styleForTitleColor.cgColor
  27. self.resetConstraint()
  28. // do{
  29. // try self.aspect_hook(#selector(resetStyle), with: AspectOptions.positionBefore, usingBlock: {
  30. // DLog("dadsdad")
  31. // })
  32. // }catch{
  33. //
  34. // }
  35. }
  36. override var type: MCNavBarType{
  37. .segment
  38. }
  39. //MARK: - Private Property
  40. private var segment: UISegmentedControl!
  41. //MARK: - Override MCNavBarDelegate
  42. override func setNavTitle(_ title: String?) {
  43. let index = Convert.toInt(title)
  44. if index.isNotNil{
  45. if index! > self.segment.numberOfSegments - 1{
  46. self.segment.selectedSegmentIndex = self.segment.numberOfSegments - 1
  47. }
  48. else{
  49. self.segment.selectedSegmentIndex = index!
  50. }
  51. }
  52. }
  53. override func setNavSubTitle(_ title: String?) {
  54. return
  55. // guard let title = title else {
  56. // return
  57. // }
  58. // let strs: [String] = title.components(separatedBy: ",")
  59. // if strs.count == segment.numberOfSegments{
  60. // for i in 0..<strs.count{
  61. // self.segment.setTitle(strs[i], forSegmentAt: i)
  62. // }
  63. // }
  64. }
  65. //MARK: -
  66. required init?(coder: NSCoder) {
  67. fatalError("init(coder:) has not been implemented")
  68. }
  69. }