// // MCNavBarSegmentView.swift // HCQuanfangtong // // Created by Apple on 2022/8/2. // Copyright © 2022 Jyp. All rights reserved. // import UIKit class MCNavBarSegmentView: MCNavBarView { init(dataSource: [String], tap: ((Int)->Void)?, style: MCNavBarStyle? = nil, defaultSelectedIndex: Int = 0){ super.init(title: nil, subTitle: nil, style: style) self.segment = UISegmentedControl.init(items: dataSource) self.segment.selectedSegmentIndex = defaultSelectedIndex self.segment.tintColor = self.navStyle.styleForBackgroundColor self.segment.backgroundColor = self.navStyle.styleForTitleColor self.segment.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: self.navStyle.styleForTitleColor], for: .selected) // self.segment.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: self.navStyle.styleForBackgroundColor], for: .normal) // self.segment.setBackgroundImage(UIImage.init(color: self.navStyle.styleForBackgroundColor).stretchableImage(withLeftCapWidth: 0, topCapHeight: 0), for: .selected, barMetrics: .default) // self.segment.setBackgroundImage(UIImage.init(color: self.navStyle.styleForTitleColor).stretchableImage(withLeftCapWidth: 0, topCapHeight: 0), for: .normal, barMetrics: .default) _ = self.segment.rx.value.takeUntil(self.rx.deallocated).subscribe(onNext:tap) self.contentView.addArrangedSubview(self.segment) self.segment.mas_makeConstraints { make in make?.height.offset()(30) } self.segment.layer.borderWidth = 1 self.segment.layer.borderColor = self.navStyle.styleForTitleColor.cgColor self.resetConstraint() // do{ // try self.aspect_hook(#selector(resetStyle), with: AspectOptions.positionBefore, usingBlock: { // DLog("dadsdad") // }) // }catch{ // // } } override var type: MCNavBarType{ .segment } //MARK: - Private Property private var segment: UISegmentedControl! //MARK: - Override MCNavBarDelegate override func setNavTitle(_ title: String?) { let index = Convert.toInt(title) if index.isNotNil{ if index! > self.segment.numberOfSegments - 1{ self.segment.selectedSegmentIndex = self.segment.numberOfSegments - 1 } else{ self.segment.selectedSegmentIndex = index! } } } override func setNavSubTitle(_ title: String?) { return // guard let title = title else { // return // } // let strs: [String] = title.components(separatedBy: ",") // if strs.count == segment.numberOfSegments{ // for i in 0..