<commit_before>//
//  BXSegmentTabViewController.swift
//  BXAppKit
//
//  Created by <NAME> on 11/05/2017.
//  Copyright © 2017 banxi1988. All rights reserved.
//

import Foundation
import UIKit



open class BXSegmentTabViewController:UIViewController,ViewControllerContainerProtocol{
  open var containerView: UIView{ return self.view }
  public var containerViewController: UIViewController{ return self}

  public private(set) var segmentIndexViewControllerMap:[Int:UIViewController] = [:]

  public let segmentControl = UISegmentedControl(frame: .zero)

  public func add(segmentTitle:String,viewController:UIViewController) -> Int{
    let index = segmentControl.numberOfSegments
    segmentIndexViewControllerMap[index] = viewController
    segmentControl.insertSegment(withTitle: segmentTitle, at: index, animated: false)
    return index
  }

  private var previousSelectedSegmentIndex = -1

  open override func viewDidLoad() {
    super.viewDidLoad()
    setupSegmentTab()
    segmentControl.addTarget(self, action: #selector(onSelectedSegmentalChanged), for: .valueChanged)
    segmentControl.sizeToFit()
    navigationItem.titleView = segmentControl
    if segmentControl.numberOfSegments > 0 {
      showSegmentTab(index: 0)
    }
  }

  /// setup segment tab before
  open func setupSegmentTab(){
    
  }

  func onSelectedSegmentalChanged(){
    showSegmentTab(index: segmentControl.selectedSegmentIndex)
  }

  public func showSegmentTab(index:Int){
    if index == previousSelectedSegmentIndex{
      return
    }
    var oldTabVC:UIViewController?
    if previousSelectedSegmentIndex > -1{
      oldTabVC = segmentIndexViewControllerMap[previousSelectedSegmentIndex]
    }<commit_msg>fix修改 segmetTabVC 的实现使 containerView 作为 view 的子 View<commit_after>//
//  BXSegmentTabViewController.swift
//  BXAppKit
//
//  Created by <NAME> on 11/05/2017.
//  Copyright © 2017 banxi1988. All rights reserved.
//

import Foundation
import UIKit



open class BXSegmentTabViewController:UIViewController,ViewControllerContainerProtocol{
  open var containerView = UIView(frame: .zero)
  public var containerViewController: UIViewController{ return self}

  public private(set) var segmentIndexViewControllerMap:[Int:UIViewController] = [:]

  public let segmentControl = UISegmentedControl(frame: .zero)

  public func add(segmentTitle:String,viewController:UIViewController) -> Int{
    let index = segmentControl.numberOfSegments
    segmentIndexViewControllerMap[index] = viewController
    segmentControl.insertSegment(withTitle: segmentTitle, at: index, animated: false)
    return index
  }

  private var previousSelectedSegmentIndex = -1

  open override func loadView() {
    super.loadView()
    view.addSubview(containerView)
    containerView.translatesAutoresizingMaskIntoConstraints = false
    containerView.pac_horizontal(0)
    containerView.pa_below(topLayoutGuide).install()
    containerView.pa_above(bottomLayoutGuide).install()
    automaticallyAdjustsScrollViewInsets = false
    
  }

  open override func viewDidLoad() {
    super.viewDidLoad()
    setupSegmentTab()
    segmentControl.addTarget(self, action: #selector(onSelectedSegmentalChanged), for: .valueChanged)
    segmentControl.sizeToFit()
    navigationItem.titleView = segmentControl
    if segmentControl.numberOfSegments > 0 {
      showSegmentTab(index: 0)
    }
  }

  /// setup segment tab before
  open func setupSegmentTab(){
    
  }

  func onSelectedSegmentalChanged(){
    showSegmentTab(index: segmentControl.selectedSegmentIndex)
  }

  public func showSegmentTab(index:Int){
    if index == previousSelectedSegmentIndex{
      return
    }
    var oldTabVC:UIViewController?
    if previousSelectedSegmentIndex > -1{
      oldTabVC = segmentIndexViewControllerMap[previousSelectedSegmentIndex]
    }
{'fraction_non_alphanumeric': 0.08066211245401997, 'fraction_numerical': 0.010772464529689963, 'mean_word_length': 4.517391304347826, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6429172', 'n_tokens_mistral': 1109, 'n_tokens_neox': 1017, 'n_words': 235}