Yalantis/Segmentio

Can't see titles IOS10

yatanadam opened this issue · 9 comments

Hi

Thanks for awesome competent.

My error is; item titles of segments aren't seen for IOS10. But everything works well for IOS11.

screen shot 2018-02-15 at 13 23 20

`

var segmentItems = [SegmentioItem]()
for item in orderedLocationList {
  let segmentItem = SegmentioItem(title: item.location, image:nil)
  segmentItems.append(segmentItem)
}


let indicatorOptions =  SegmentioIndicatorOptions(type: .bottom, ratio: 1.0, height: 3.0, color: .focepGreenColor())

let states = SegmentioStates(
  defaultState: SegmentioState(
    backgroundColor: .clear,
    titleFont: UIFont.systemFont(ofSize: 15),
    titleTextColor: .black
  ),
  selectedState: SegmentioState(
    backgroundColor: .clear,
    titleFont: UIFont.systemFont(ofSize: 15),
    titleTextColor: .focepGreenColor()
  ),
  highlightedState: SegmentioState(
    backgroundColor: .clear,
    titleFont: UIFont.systemFont(ofSize: 15),
    titleTextColor: .focepGreenColor()
  )
)

let  horizontalSeparatorOptions = SegmentioHorizontalSeparatorOptions(
  type: SegmentioHorizontalSeparatorType.topAndBottom, // Top, Bottom, TopAndBottom
  height: 0,
  color: .gray
)

locationSegmentedControl.setup(
  content: segmentItems ,
  style: SegmentioStyle.onlyLabel,
  options: SegmentioOptions(backgroundColor: .white, segmentPosition: .fixed(maxVisibleItems: 3), scrollEnabled: false, indicatorOptions: indicatorOptions, horizontalSeparatorOptions:  horizontalSeparatorOptions , verticalSeparatorOptions: nil, imageContentMode: .center, labelTextAlignment: .center, labelTextNumberOfLines: 1, segmentStates:states, animationDuration: 0.25)
)

locationSegmentedControl.valueDidChange = { segmentio, segmentIndex in
  self.segmentedControlValueDidChange(segmentIndex: segmentIndex)
}

`

Same issue when segmentioView is loaded in viewDidLoad.

Did it get resolved? @yatanadam @grifas

@chetan5476 I initialised it in viewDidAppear and that works.

@grifas I tried doing that too but dint work for me.. so I took other library.

also here, same issue.
Loading from xib loads the control "fine" you can see how it kinda scrolls up to the right position, but after selecting some tab, the labels scrolls down and are right under the bottom border. I tried disabling in the xib "safe areas". also loading the segmented control on viewDidAppear. But nothing seems to help.

Same issue. Any suggestion on how to fix the issue?

I didn't do anything and now that works when I initialised it in the viewDidLoad.

here my code:

class MultipleViewController: UIViewController {
	
	var segmentedControl = Segmentio(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44))
	var containerView = UIView()
	var scrollView = UIScrollView()
	
	var titles: [String]?
	var images: [UIImage]?
	var controllers: [UIViewController]?
	
	init(titles: [String]? = nil, images: [UIImage]? = nil, and controllers: [UIViewController]? = nil) {
		super.init(nibName: nil, bundle: nil)
		
		self.titles = titles
		self.images = images
		self.controllers = controllers
	}
	
	required init?(coder aDecoder: NSCoder) {
		fatalError("init(coder:) has not been implemented")
	}
	
	override func viewDidLoad() {
		super.viewDidLoad()
		
		initialize()
		setupScrollView()
		
		if let titles = titles {
			SegmentioBuilder.buildSegmentioView(segmentioView: segmentedControl, content: titles, segmentioStyle: .onlyLabel)
		} else if let images = images {
			SegmentioBuilder.buildSegmentioView(segmentioView: segmentedControl, content: images, segmentioStyle: .onlyImage)
		}
		
		segmentedControl.selectedSegmentioIndex = 0
		segmentedControl.valueDidChange = { [weak self] _, segmentIndex in
			print("Selected item: ", segmentIndex)
			
			if let scrollViewWidth = self?.scrollView.frame.width {
				let contentOffsetX = scrollViewWidth * CGFloat(segmentIndex)
				
				self?.scrollView.setContentOffset(CGPoint(x: contentOffsetX, y: 0), animated: true)
			}
		}
	}
	
	func initialize() {
		scrollView.accessibilityLabel = "scrollView"
		scrollView.isPagingEnabled = true
		scrollView.showsHorizontalScrollIndicator = false
		scrollView.bounces = false
		
		containerView.addSubview(scrollView)
		view.addSubview(containerView)
		view.addSubview(segmentedControl)
		
		containerView.snp.makeConstraints { (make) in
			if #available(iOS 11, *) {
				make.left.right.bottom.equalTo(view.safeAreaLayoutGuide)
				make.top.equalTo(segmentedControl.snp.bottom)
			} else {
				make.left.right.equalToSuperview()
				make.top.equalTo(segmentedControl.snp.bottom)
				make.bottom.equalTo(self.bottomLayoutGuide.snp.top)
			}
		}
		
		scrollView.snp.makeConstraints { (make) in
			make.edges.equalToSuperview()
		}
	}
	
	private func setupScrollView() {
		guard let controllers = controllers else { return }
		
		scrollView.delegate = self
		
		scrollView.contentSize = CGSize(
			width: UIScreen.main.bounds.width * CGFloat(controllers.count),
			height: containerView.frame.height
		)
		
		for (index, viewController) in controllers.enumerated() {
			viewController.view.frame = CGRect(
				x: UIScreen.main.bounds.width * CGFloat(index),
				y: 0,
				width: scrollView.frame.width,
				height: scrollView.frame.height
			)
			addChildViewController(viewController)
			scrollView.addSubview(viewController.view, options: .useAutoresize) // module's extension
			viewController.didMove(toParentViewController: self)
		}
	}
}

Hope it helps you guys.

Hello, for those interested, this worked for me..

I reworked my main container viewcontroller's view.
Before I had components in a stackview, and I tried to do it. "old fashion" way without the stackview. Results, now it works as expected.
I was debugging the code a little bit to see what was happening. And when the call to scrollToVisibleRect on the collection view used, the content offset was to -20 on the Y axis. thats why the resulted behaviour. Funny thing was that the passed rect contained origin Y value to 0. Seems scrollview inside somehow in a stack view are not very good friends.

I hope this info helps

I get this error on console

2018-07-26 11:06:45.708 SDK Test App[20547:211414] the behavior of the UICollectionViewFlowLayout is not defined because:
2018-07-26 11:06:45.708 SDK Test App[20547:211414] the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
2018-07-26 11:06:45.708 SDK Test App[20547:211414] Please check the values return by the delegate.
2018-07-26 11:06:45.709 SDK Test App[20547:211414] The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7fe4c042e9c0>, and it is attached to <Segmentio.MyCollectionView: 0x7fe4c2091600; baseClass = UICollectionView; frame = (0 0; 414 54); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x7fe4c042f920>; layer = <CALayer: 0x7fe4bff08cb0>; contentOffset: {0, 0}; contentSize: {600, 54}> collection view layout: <UICollectionViewFlowLayout: 0x7fe4c042e9c0>.
2018-07-26 11:06:45.709 SDK Test App[20547:211414] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.

It happens only in iOS 9 and 10. iOS 11 works fine.
Seems that Segmentio's collection view gets extra 49pt bottom content inset.
That's because UIKit wants to use nice translucent bar for tab bar and navigation bar.

To fix that you need to disable Adjust Scroll Views Insets.
I didn't check how to keep adjusted insets for all scroll views but not Segmentio's.

screen shot 2018-07-26 at 11 05 16