Posts

Showing posts with the label uisegmentedcontrol

How can I subclass UISegmentedControl having custom designated initializer?

How can I subclass UISegmentedControl having custom designated initializer? Seems like a trivial issue but I am not able to make this compile. Neither in playgrounds nor in normal swift ios projects. (Please note I am not using Storyboards that's why I don't need / care about the init?(coder) part..it;s jsut it has to be include otherwise the complier complains about it.) class SegmentedControl: UISegmentedControl { let configuration: [String] required init(configuration: [String]) { self.configuration = configuration super.init(items: configuration) } required init?(coder aDecoder: NSCoder) { fatalError() } } let x = SegmentedControl(configuration: ["a","b"]) It is complaining about not having the deisignated initializer implemented. Fatal error: Use of unimplemented initializer 'init(frame:)' for class '__lldb_expr_167.SegmentedControl' I don't understand what is going on here. Isn...

UISegmentedControl maintain aspect ratio on image

Image
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: 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 3 Answers 3 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 } } Please add an explanation. ...