ios - 在 Swift 3 中滚动动态 UISegmentedControl

标签 ios swift uisegmentedcontrol human-interface

您好,我想创建一个包含 20 多个项目的动态 UISegmented View 。我试过了,但输出是这样的。

enter image description here

文本被切片,不完全可见。我希望它向左和向右滚动并显示全文。有人可以帮我解决这个问题吗?发送

View Controller

override func viewDidLoad() {
    super.viewDidLoad()

    let items = ["All Fruits", "Orange", "Grapes", "Banana",  "Mango", "papaya", "coconut", "django"]
    let filtersSegment = UISegmentedControl(items: items)
    filtersSegment.frame = CGRect.init(x: 10, y: 60, width: 300, height: 50)
    filtersSegment.selectedSegmentIndex = 0
    filtersSegment.tintColor = UIColor.black
    filtersSegment.addTarget(self, action: #selector(self.filterApply), for: UIControlEvents.valueChanged)
    self.view.addSubview(filtersSegment)
}

@objc private func filterApply(segment:UISegmentedControl) -> Void {
    print("Selected Index : \(segment.selectedSegmentIndex)")
}

最佳答案

你可以试试

[[UILabel appearanceWhenContainedIn:[UISegmentedControl class], nil] setNumberOfLines:0];

segment.apportionsSegmentWidthsByContent = YES;

swift :

有点像,

   UILabel.appearance(whenContainedInInstancesOf: [UISegmentedControl.self]).numberOfLines = 0
   segment.apportionsSegmentWidthsByContent = true

您可以在 ScrollView 中使用分段控件。但是用 UISegmentedControl你不应该使用超过四个或五个段。它的主要目的是显示三个或四个选项!你应该尝试不同的机制来实现你的目标!

你应该引用this post如果它能有所帮助,也可以!

关于ios - 在 Swift 3 中滚动动态 UISegmentedControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46519573/

相关文章:

c - 整数不作为对 Swift 中函数的引用传递

ios - 我想从 CoreData 获取数据,如果数据存在我想返回 true,如果不存在我想返回 false

swift - 解决文本字段和按钮图像问题带来的问题

objective-c - 分段控制

iphone - MPMoviePlayerController 在连续视频之间产生闪烁

ios - UITableViewCell 无法使用 systemLayoutSizeFittingSize 获得正确的宽度

iphone - 使用分段控件在 2 个 View 之间切换

ios - UISegmentedControl 段大小触摸检测

ios - 需要帮助弄清楚如何避免 iOS 中的崩溃

objective-c - 无法运行 performSelector : afterDelay from CoreMotion Block