如何以编程方式将UISegmentedControl添加到容器视图

Ale*_*ede 60 iphone objective-c uisegmentedcontrol ios swift

我如何定义框架UISegmentedControl?我希望分段控件出现在container viewie 的底部UIView.

Muh*_*wan 181

这个是完美的我测试.....

UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 435)];
 scroll.contentSize = CGSizeMake(320, 700);
 scroll.showsHorizontalScrollIndicator = YES;

 NSArray *itemArray = [NSArray arrayWithObjects: @"One", @"Two", @"Three", nil];
 UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
 segmentedControl.frame = CGRectMake(35, 200, 250, 50);
 segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
 [segmentedControl addTarget:self action:@selector(MySegmentControlAction:) forControlEvents: UIControlEventValueChanged];
 segmentedControl.selectedSegmentIndex = 1;     
 [scroll addSubview:segmentedControl];
 [segmentedControl release]; 
 [self.view addSubview:scroll];
Run Code Online (Sandbox Code Playgroud)

然后在您的课程中添加您的方法.

- (void)MySegmentControlAction:(UISegmentedControl *)segment 
{    
        if(segment.selectedSegmentIndex == 0)
        {
            // code for the first button
        } 
}
Run Code Online (Sandbox Code Playgroud)

对于已弃用的UISegmentedControlStyle,您可以查看 URL.

  • 2年即将过去,但答案仍未被接受!虽然帮助了我!谢谢! (5认同)
  • @SOFY我认为他已经死了或因为不接受答案而入狱.:-) (5认同)
  • 请将此答案标记为正确,如果您获得成功,这将对其他人有所帮助. (2认同)

Mad*_*dhu 19

你可以这样做......

UISegmentedControl *segmentControl = [[UISegmentedControl alloc]initWithItems:@[@"One",@"Two"]];

[segmentControl setSegmentedControlStyle:UISegmentedControlStyleBar];
segmentControl.frame = CGRectMake(10, 50, 300, 30);
[segmentControl addTarget:self action:@selector(segmentedControlValueDidChange:) forControlEvents:UIControlEventValueChanged];
[segmentControl setSelectedSegmentIndex:0];
[scrollView addSubview:segmentControl];
[segmentControl release];
Run Code Online (Sandbox Code Playgroud)

第2步:

-(void)segmentedControlValueDidChange:(UISegmentedControl *)segment
{
switch (segment.selectedSegmentIndex) {
    case 0:{
        //action for the first button (Current)
        break;}
    case 1:{
        //action for the first button (Current)
        break;}
    }
}
Run Code Online (Sandbox Code Playgroud)


Cod*_*der 16

更新了Swift 4.x的答案:

class SegmentClass: UIViewController {
    func addControl()  {
        let items = ["Uno", "Dos", "Tres"]
        let segmentedControl = UISegmentedControl(items: items)
        segmentedControl.frame = CGRect(x: 35, y: 200, width: 250, height: 50)
        segmentedControl.addTarget(self, action: #selector(segmentAction(_:)), for: .valueChanged)
        segmentedControl.selectedSegmentIndex = 1
        view.addSubview(segmentedControl)
    }

    @objc func segmentAction(_ segmentedControl: UISegmentedControl) {
        switch (segmentedControl.selectedSegmentIndex) {
        case 0:
            break // Uno
        case 1:
            break // Dos
        case 2:
            break // Tres
        default:
            break
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Objective-C中的原始答案:

NSArray *itemArray = [NSArray arrayWithObjects: @"Uno", @"Dos", @"Tres", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(35, 200, 250, 50);
[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents: UIControlEventValueChanged];
segmentedControl.selectedSegmentIndex = 1;
[self.view addSubview:segmentedControl];
Run Code Online (Sandbox Code Playgroud)
  1. 创建一个数组来存储段的值
  2. 使用数组初始化段
  3. 在屏幕上为其指定一个位置并调整控件的大小
  4. 将其指向用户与其交互时调用的方法
  5. 选择默认值(在本例中为Dos)
  6. 将它放在主视图上

然后创建在用户更改值时调用的segmentAction方法

- (void)segmentAction:(UISegmentedControl *)segment
{
    switch (segment.selectedSegmentIndex) {
        case 0:
            // Uno
            break;
        case 1:
            // Dos
            break;
        case 2:
            // Tres
            break;
        default:
            break;
    }
}
Run Code Online (Sandbox Code Playgroud)

我只是喜欢switch语句,因为它看起来更干净.您可以通过创建枚举并将其中的值用于选项(optionUno,optionDos,optionTres)而不是0,1,2来改进它.


Raj*_*han 7

步骤1.使用索引值创建段控件

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"common-bg.jpg"]];
    [self.navigationItem setHidesBackButton:YES];

    //-- For creating segment control in navigation bar
     UISegmentedControl *mainSegment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Back", @"Month", @"Year", @"Home", nil]];
    [mainSegment setSegmentedControlStyle:UISegmentedControlStyleBar];
    mainSegment.frame = CGRectMake(0,0, 400, 43);
    self.navigationItem.titleView = mainSegment;
    mainSegment.selectedSegmentIndex = 1;
    [mainSegment addTarget:self action:@selector(mainSegmentControl:) forControlEvents: UIControlEventValueChanged];
    [self.view addSubview:mainSegment];
    //--**--

}
Run Code Online (Sandbox Code Playgroud)

第2步.创建子视图

- (void)mainSegmentControl:(UISegmentedControl *)segment
{

    if(segment.selectedSegmentIndex == 0)
    {
        // action for the first button (Current or Default)
    }
    else if(segment.selectedSegmentIndex == 1)
    {
        // action for the second button 
    }
    else if(segment.selectedSegmentIndex == 2)
    {
        // action for the third button 
    } 
    else if(segment.selectedSegmentIndex == 3)
    {
        // action for the fourth button 
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 6

要以编程方式将UISegmentedControl添加到容器视图,请执行以下步骤:

// Create UISegmentedControl object to add control UISegment.
UISegmentedControl *objSegment = [[UISegmentedControl alloc] initWithItems:array];

// Set frame for objSegment Control (formate: (x, y, width, height)). where, y = (height of view - height of control). 
[objSegment setFrame:CGRectMake(0, (self.view.frame.size.height - 40), 320, 40)];

// handle UISegmentedControl action.
[objSegment addTarget:self action:@selector(handleSegmentControl:) forControlEvents: UIControlEventValueChanged];

 // Add your UISegmentedControl in your view.
[self.view addSubview:objSegment];
Run Code Online (Sandbox Code Playgroud)

如果您有任何疑问,请与我联系.


Hem*_*ang 5

迅速:

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

@objc private func filterApply(segment: UISegmentedControl) -> Void {
    switch segment.selectedSegmentIndex {
    case 1:
        //Do something for Orange
    case 2:
        //Do something for Grapes
    case 3:
        //Do something for Banana
    default:
        //Do something for All Fruits
    }
}
Run Code Online (Sandbox Code Playgroud)