ios 如何以编程方式将 UISegmentedControl 添加到容器视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6688160/
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
How to programmatically add a UISegmentedControl to a container view
提问by Alede
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
.
我将如何定义 a 的框架UISegmentedControl
?我希望分段控件出现在container view
ie的底部UIView
。
回答by Muhammad Rizwan
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
}
}
For deprecated UISegmentedControlStyle you can take a look on thisURL.
对于已弃用的 UISegmentedControlStyle,您可以查看此URL。
回答by Madhu
U can do like this...
你可以这样做...
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];
Step 2:
第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;}
}
}
回答by CodeBender
Updated answer for Swift 4.x:
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
}
}
}
Original answer in Objective-C:
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];
- Create an array to store the values for the segment
- Initialize the segment using the array
- Assign it a location on the screen & size the control
- Point it towards a method that is called when the user interacts with it
- Select a default value (in this case, Dos)
- Place it on the main view
- 创建一个数组来存储段的值
- 使用数组初始化段
- 在屏幕上为其分配一个位置并调整控件的大小
- 将其指向用户与其交互时调用的方法
- 选择默认值(在本例中为 Dos)
- 把它放在主视图上
Then create the segmentAction method that is called when the user changes a value
然后创建当用户更改值时调用的 segmentAction 方法
- (void)segmentAction:(UISegmentedControl *)segment
{
switch (segment.selectedSegmentIndex) {
case 0:
// Uno
break;
case 1:
// Dos
break;
case 2:
// Tres
break;
default:
break;
}
}
I just prefer the switch statement because it is cleaner to look at. You can improve it by creating an enum and using the values in it for the options (optionUno,optionDos,optionTres) instead of 0,1,2.
我只是更喜欢 switch 语句,因为它看起来更清晰。您可以通过创建一个枚举并使用其中的值作为选项 (optionUno,optionDos,optionTres) 而不是 0,1,2 来改进它。
回答by Rajesh Loganathan
Step 1.Create segment control with index values
步骤 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];
//--**--
}
Step 2.Create subview
步骤 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
}
}
回答by Rajesh Loganathan
To add a UISegmentedControl to a container view programmatically, follow following steps:
要以编程方式将 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];
If you have any query, Contact me.
如果您有任何疑问,请联系我。
回答by Hemang
Swift:
迅速:
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
}
}
回答by Evana
This will work for all type of iOS device:
这适用于所有类型的 iOS 设备:
UISegment *segment = [[UISegmentedControl alloc] initWithItems:array];
segment.frame = CGRectMake(0, self.view.frame.size.height - 40, 300, 40);
UIFont *font = [UIFont fontWithName:@"DroidSans" size:18.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
forKey:NSFontAttributeName];
[segment setTitleTextAttributes:attributes
forState:UIControlStateNormal];
[segment addTarget:self action:@selector(segmentAction:) forControlEvents: UIControlEventValueChanged];
[self.view addSubview:segment];