【问题标题】:Aspect fit image in UISegmentedControlUISegmentedControl 中的方面适合图像
【发布时间】:2019-04-15 16:55:35
【问题描述】:
我有一个UISegmentedControl 控件,其中包含 4 个图像。我希望这些与Aspect fit 一起扩展。
在按钮上有您可以执行的button.imageView?.contentMode = .scaleAspectFit,但我似乎无法为细分获取imageView。
如何做到这一点?
以编程方式或情节提要无关紧要,因此感谢您的帮助!
【问题讨论】:
标签:
ios
swift
uisegmentedcontrol
【解决方案1】:
你需要遍历segmentControl内的所有UIImageView。
segmentedControl.subviews.flatMap{$0.subviews}.forEach { subview in
if let imageView = subview as? UIImageView, imageView.frame.width > 5 {
imageView.contentMode = .scaleAspectFit
}
}
解释:
imageView.frame.width > 5
SegmentControl内部有很多不同的imageView,所以我们检查UIImageView的宽度以确保它是segment的图像。