Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISegmentedControl maintain aspect ratio on image

I'm having a hard time making images on a UISegmentedControl keep their aspect ratio and not scale to all sides. Here's what it looks like: UiSegmentedControl maintain aspect ratio

The images are 100X100(except nr 6 gonna replace soon) and I want them to maintain the aspect ratio.

I have tried:

self.segment.contentMode = .ScaleAspectFit
//and:
self.segment.contentMode = UIViewContentMode.Center

Are there a way I can target the UIImageView in the segment..that way I can set the contentMode..right? Or is there another approach?

Thanks

like image 429
JoakimE Avatar asked Aug 13 '16 20:08

JoakimE


1 Answers

segmentedControl.subviews.flatMap{$0.subviews}.forEach { subview in
    if let imageView = subview as? UIImageView, let image = imageView.image, image.size.width > 5 {
        // The imageView which isn't separator
        imageView.contentMode = .scaleAspectFit
    }
}
like image 174
Trần Minh Quang Avatar answered Sep 22 '22 23:09

Trần Minh Quang