ios UISegmentedControl 以编程方式更改段数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11638861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:25:43  来源:igfitidea点击:

UISegmentedControl change number of segments programmatically

iosuikituisegmentedcontrol

提问by Beppino66

Is there a way to change the number of segments programmatically?

有没有办法以编程方式更改段数?

回答by Scar

Yes, you can use

是的,你可以使用

removeSegmentAtIndex:(NSUInteger) animated:(BOOL)

And

insertSegmentWithTitle:(NSString *) atIndex:(NSUInteger) animated:(BOOL)

回答by Zorayr

To replace the segments entirely, you can use the following function:

要完全替换段,您可以使用以下函数:

- (void)setSegments:(NSArray *)segments
{
    [segmentController removeAllSegments];

    for (NSString *segment in segments) {
        [segmentController insertSegmentWithTitle:segment atIndex:segmentController.numberOfSegments animated:NO];
    }
}

Hope this helps.

希望这可以帮助。

回答by kernelpanic

And here's a little Swift extension to replace current segmentedControl with array of new values

这是一个小的 Swift 扩展,用于用新值数组替换当前的 segmentedControl

Swift 3

斯威夫特 3

extension UISegmentedControl {
    func replaceSegments(segments: Array<String>) {
        self.removeAllSegments()
        for segment in segments {
            self.insertSegmentWithTitle(segment, atIndex: self.numberOfSegments, animated: false)
        }
    }
}

Swift 4

斯威夫特 4

extension UISegmentedControl {
    func replaceSegments(segments: Array<String>) {
        self.removeAllSegments()
        for segment in segments {
            self.insertSegment(withTitle: segment, at: self.numberOfSegments, animated: false)
        }
    }
}

回答by simon_smiley

For the sake of completeness (and because I ended up here looking for how to achieve the same thing in xib) here is how to do it in xib:

为了完整起见(并且因为我最终在这里寻找如何在 xib 中实现相同的事情)这里是如何在 xib 中执行此操作:

enter image description here

在此处输入图片说明

回答by simon_smiley

Here is a Swift extension for replacing the segments with a sequence of strings. It's similar to another answergiven here except it can be used with any sequence, meaning you can also pass in slices, sets, etc.

这是一个用字符串序列替换段的 Swift 扩展。它类似于此处给出的另一个答案,但它可以与任何序列一起使用,这意味着您还可以传入切片、集合等。

extension UISegmentedControl {

    /// Replace the current segments with new ones using a given sequence of string.
    /// - parameter withTitles:     The titles for the new segments.
    public func replaceSegments<T: Sequence>(withTitles: T) where T.Iterator.Element == String {
        removeAllSegments()
        for title in withTitles {
            insertSegment(withTitle: title, at: numberOfSegments, animated: false)
        }
    }
}

回答by Faizal Nowshad KN

adding and deleting segments in swift4 using code

使用代码在 swift4 中添加和删除段

class ViewController: UIViewController {

  @IBOutlet weak var segment1: UISegmentedControl!

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
  }

  @IBAction func insert(_ sender: Any) {  
    segment1.insertSegment(withTitle: "\(segment1.numberOfSegments+1)", at: segment1.numberOfSegments, animated: true)  
  }

  @IBAction func remove(_ sender: Any) {  
    segment1.removeSegment(at: segment1.numberOfSegments-1, animated: true)
  }
}

回答by Pablo Ruan

work for me, UIsegmentedControll contains two segments, i want add one in index 2, use this code in swift 2.2 use:

为我工作,UIsegmentedControll 包含两个段,我想在索引 2 中添加一个,在 swift 2.2 中使用此代码使用:

SEG_TipoFiltro.insertSegmentWithTitle("Title", atIndex: 2, animated: false)