Convert a specific segment in segmented management

Is there a way to convert the contents of a specific segment contained in a segmented control? (I have to rotate it 180 degrees)

Image not contained in it, because in my case there is none (I used FontAwesome)

Demonstration:

from: original : rotated

+5
source share
2 answers

Yes there is!

Consider that we have a segmented control that contains 3 segments:

enter image description here

This is actually a view that contains 3 subviews typed as a UISegment . If we try to print this segmented subviews control (suppose we connected it to the desired view controller as segmentedControl IBOutlet):

 class ViewController: UIViewController { // MARK:- IBOutlets @IBOutlet weak var segmentedControl: UISegmentedControl! override func viewDidLoad() { super.viewDidLoad() for view in segmentedControl.subviews { print(view) } } } 

The log should display something similar to:

UISegment: 0x7febebc06980; frame = (229 0; 114 29); layer = CALayer: 0x60000002edc0 UISegment: 0x7febebe140c0; frame = (114 0; 114 29); layer = CALayer: 0x618000231800 UISegment: 0x7febebe14d30; frame = (0 0; 113 29); layer = CALayer: 0x6180002319c0

This way you can access segments in a segmented control as a UISegment , so apply the necessary editing to it as a UIView. For example, if you want to rotate the second segment, you can implement:

 override func viewDidLoad() { super.viewDidLoad() for view in segmentedControl.subviews[1].subviews { print(view) view.transform = CGAffineTransform(scaleX: -1, y: 1) } } 

As you can see, the segment also has subviews:

UISegmentLabel: 0x7f8b51701770; frame = (0 0; 64 22.5); text = "Second"; opaque = NO; userInteractionEnabled = NO; layer = _UILabelLayer: 0x610000091530

UIImageView: 0x7f8b51701470; frame = (114 0; 1 1); alpha = 0; opaque = NO; autoresize = LM; userInteractionEnabled = NO; tag = -1030; layer = CALayer: 0x610000031480

So, you must rotate all of these subzones, as implemented above the code snippet.

The conclusion will be:

enter image description here


The same approach should be applied with segmented controls that have images instead of names:

Before rotation:

enter image description here

After rotation:

enter image description here

+4
source

A segment is a small UIView, you can just apply the transform to it.

Refer to this question about getting a preview:

UISegmentedControl segment color selected

And refer to this other to β€œrotate” the contents.

How to rotate a UIView using transform

Then again, it will be easier for me to make an image out of it and use it instead.

+1
source

Source: https://habr.com/ru/post/1269544/


All Articles