0

I want both tabs(Hello World, Tab) equal width of controller width . I put the complete code of my tabs. if anyone have suggestion for it. plz give suggestion.i am waiting. Comment & Answer Fast.

enter image description here

import UIKit
import CarbonKit

class ViewController: UIViewController, CarbonTabSwipeNavigationDelegate {

func generateImage(for view: UIView) -> UIImage? {
    defer {
        UIGraphicsEndImageContext()
    }
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, UIScreen.main.scale)
    if let context = UIGraphicsGetCurrentContext() {
        view.layer.render(in: context)
        return UIGraphicsGetImageFromCurrentImageContext()
    }
    return nil
}

var iconWithTextImage: UIImage {
    let button = UIButton()
    let icon = UIImage(named: "home")
    button.setImage(icon, for: .normal)
    button.setTitle("Home", for: .normal)
    button.setTitleColor(UIColor.blue, for: .normal)
    button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
    button.contentEdgeInsets = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 10)
    button.imageEdgeInsets = UIEdgeInsets(top: 0, left: -10, bottom: 0, right: 10)
    button.sizeToFit()
    return generateImage(for: button) ?? UIImage()
}

override func viewDidLoad() {
    super.viewDidLoad()

    let tabSwipe = CarbonTabSwipeNavigation(items: ["HELLO WORLD", "Tab"], delegate: self)
    tabSwipe.setTabExtraWidth(40)
    tabSwipe.insert(intoRootViewController: self)

}

func carbonTabSwipeNavigation(_ carbonTabSwipeNavigation: CarbonTabSwipeNavigation, viewControllerAt index: UInt) -> UIViewController {
    guard let storyboard = storyboard else { return UIViewController() }
    if index == 0 {
        return storyboard.instantiateViewController(withIdentifier: "FirstViewController")
    }
    return storyboard.instantiateViewController(withIdentifier: "SecondTableViewController")
}

}

2 Answers2

4

This helps you.

First of all set carbonSegmentControl frame to Screen equal Width like this.

var frameRect: CGRect = (carbonTabSwipeNavigation.carbonSegmentedControl?.frame)!
frameRect.size.width = UIScreen.main.bounds.size.width
carbonTabSwipeNavigation.carbonSegmentedControl?.frame = frameRect

After that write this line for equal width.

carbonTabSwipeNavigation.carbonSegmentedControl?.apportionsSegmentWidthsByContent = false

1. Before Equal Width

enter image description here

2. After Equal Width

enter image description here

Kuldeep
  • 4,466
  • 8
  • 32
  • 59
  • 1
    @veerendrapratapsingh, remove this line `tabSwipe.setTabExtraWidth(40)` and after `tabSwipe.insert(intoRootViewController: self)` line add above 4 lines. `var frameRect: CGRect = (tabSwipe.carbonSegmentedControl?.frame)! frameRect.size.width = UIScreen.main.bounds.size.width tabSwipe.carbonSegmentedControl?.frame = frameRect tabSwipe.carbonSegmentedControl?.apportionsSegmentWidthsByContent = false` – Kuldeep Mar 19 '18 at 11:22
  • Superb @Kuldeep, thanks for your answer. I wonder if the author of CarbonKit adds right to left tabs initiation for languages like arabic into the library. – Md Rais Apr 10 '19 at 16:51
0

Before insert to root view controller:

carbonTabSwipeNavigation.carbonSegmentedControl.setWidth(view.frame.width / 2, forSegmentAt : 0)

carbonTabSwipeNavigation.carbonSegmentedControl.setWidth(view.frame.width / 2, forSegmentAt : 1)

This will set half of view width to each of the segments.

Bojan Bozovic
  • 872
  • 16
  • 35
Sarah
  • 1