分段控件提供一栏按钮(有时称为按钮栏),但只能激活其中一个按钮。分段控件会导致用户在屏幕上看到的内容发生变化。它们常用于在不同类别的信息之间选择,或在不同的应用屏幕之间切换。下面介绍基本属性和基本方法的使用。
[代码]
// ViewController.m
// UISegement
//
// Created by TingFengZhe on 15/12/28.
// Copyright © 2015年 aoyolo.com. All rights reserved.
//
#import "ViewController.h"
#import "FirstTableView.h"
#import "SecondTableView.h"
@interface ViewController ()
@property(nonatomic,strong)FirstTableView *firstView;
@property(nonatomic,strong)SecondTableView *secondView;
@property(nonatomic,strong)UIView *thirdView;
@property(nonatomic,strong)NSArray *dataSouce;
@end
@implementation ViewController
#define screenWidth [[UIScreen mainScreen]bounds].size.width
#define screenHeight [[UIScreen mainScreen]bounds].size.height
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *array = @[@"one",@"two",@"three"];
UISegmentedControl *segc = [[UISegmentedControl alloc]initWithItems:array];
segc.frame = CGRectMake(0, 20, screenWidth, 30);
//默认选择项索引
segc.selectedSegmentIndex = 1;
//有基本四种样式
/*
//设置样式
segc.segmentedControlStyle = UISegmentedControlStylePlain;
segc.segmentedControlStyle = UISegmentedControlStyleBordered;
segc.segmentedControlStyle = UISegmentedControlStyleBar;
segc.segmentedControlStyle = UISegmentedControlStyleBezeled;
*/
/*
segc.momentary = YES;//设置在点击后是否恢复原样
[segc setTitle:@"two" forSegmentAtIndex:1];//设置指定索引的题目
[segc setImage:[UIImage imageNamed:@"btn_jyy.png"] forSegmentAtIndex:3];//设置指定索引的图片
[segc insertSegmentWithImage:[UIImage imageNamed:@"mei.png"] atIndex:2 animated:NO];//在指定索引插入一个选项并设置图片
[segc insertSegmentWithTitle:@"insert" atIndex:3 animated:NO];//在指定索引插入一个选项并设置题目
[segc removeSegmentAtIndex:0 animated:NO];//移除指定索引的选项
[segc setWidth:70.0 forSegmentAtIndex:2];//设置指定索引选项的宽度
[segc setContentOffset:CGSizeMake(10.0,10.0) forSegmentAtIndex:4];//设置选项中图片等的左上角的位置
*/
[segc addTarget:self action:@selector(segcAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segc];
//添加子视图
[self.view addSubview:self.thirdView];
[self.view addSubview:self.secondView];
[self.view addSubview:self.firstView];
}
- (void)segcAction:(UISegmentedControl *)segc{
NSLog(@"%@",self.view.subviews);
switch (segc.selectedSegmentIndex) {
case 0:
//将firstView移动到视图层的第一位
[self.view bringSubviewToFront:self.firstView];
break;
case 1:
[self.view bringSubviewToFront:self.secondView];
break;
case 2:
[self.view bringSubviewToFront:self.thirdView];
break;
default:
break;
}
}
#pragma mark -初始化UI
- (FirstTableView *)firstView{
if (!_firstView) {
_firstView = [[FirstTableView alloc]initWithFrame:CGRectMake(0, 55, screenWidth, screenHeight-50) style:UITableViewStylePlain];
// _firstView.data = self.dataSouce;
_firstView.backgroundColor = [UIColor orangeColor];
}
return _firstView;
}
- (SecondTableView *)secondView{
if (!_secondView) {
_secondView = [[SecondTableView alloc]initWithFrame:CGRectMake(0, 55, screenWidth, screenHeight-50) style:UITableViewStylePlain];
// _secondView.data = self.dataSouce;
_secondView.backgroundColor = [UIColor purpleColor];
}
return _secondView;
}
- (UIView *)thirdView{
if (!_thirdView) {
_thirdView = [[UIView alloc]initWithFrame:CGRectMake(0, 55, screenWidth, screenHeight-50)];
_thirdView.backgroundColor = [UIColor cyanColor];
}
return _thirdView;
}
#pragma mark-dataSouce
- (NSArray *)dataSouce{
if (!_dataSouce) {
_dataSouce = [UIFont familyNames];
}
return _dataSouce;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
特此声明:本博客内容仅限于个人学习和交流,不涉及商业和其他,如有侵权请提前告知