如何以编程方式将UISegmentedControl添加到容器视图 [英] How to programmatically add a UISegmentedControl to a container view

查看:127
本文介绍了如何以编程方式将UISegmentedControl添加到容器视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为 UISegmentedControl 定义框架?
我希望分段控件出现在容器视图的底部,即 UIView

How would I define the frame for a UISegmentedControl? I would like the segmented control to appear at the bottom of a container view i.e UIView.

推荐答案

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

this one is perfect i tested.....

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];

然后在你的班级中添加你的方法。

Then add your method in your class.

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

对于已弃用的UISegmentedControlStyle,您可以看看这个
网址。

For deprecated UISegmentedControlStyle you can take a look on this URL.

这篇关于如何以编程方式将UISegmentedControl添加到容器视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆