UIday0601:UISegmentedControl的属性和用法

本文介绍如何使用UISegmentedControl实现视图切换功能,并详细解释了如何设置样式、添加事件监听及常用的方法调用。

Segment相当于集成了若干个button

通常我们会点击不同的Segment来切换不同的View

addTarget: action: forControlEvents: 给 UISegmentedControl 添加事件,controlEvent为UIControlEventValueChanged


AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    RootViewController *rootVC = [[RootViewController alloc]init];
    self.window.rootViewController = rootVC;
    
    return YES;
}



RootViewController.m

#import "RootViewController.h"

@interface RootViewController ()

@property(nonatomic,strong)RootView * rv;

@end

@implementation RootViewController

-(void)loadView{
    self.rv = [[RootView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = _rv;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //添加事件
    [self.rv.segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    
    // 开始选中 设置默认选中
    self.rv.segment.selectedSegmentIndex = 2;
    
    // 瞬间显示 用的情况少
    self.rv.segment.momentary = YES;
    
    // 是否自适应宽度,用的情况少 按标签字数自适应显示宽度,默认为NO
    self.rv.segment.apportionsSegmentWidthsByContent = NO;
    
    // 插入标题  animated动画默认为NO
    [self.rv.segment insertSegmentWithTitle:@"test" atIndex:0 animated:YES];
    
    // 删除分段标题
    [self.rv.segment removeSegmentAtIndex:0 animated:YES];
    
    // 设置分段
    [self.rv.segment setTitle:@"0000" forSegmentAtIndex:1];
    
    // 设置分段宽度,同时会挤压相邻分段的宽度
    [self.rv.segment setWidth:120 forSegmentAtIndex:1];
    
    // 设置title的偏移位置,默认是居中的
    [self.rv.segment setContentOffset:CGSizeMake(10, 10) forSegmentAtIndex:0];
    
    // 设置能否使用 默认为YES
    [self.rv.segment setEnabled:NO forSegmentAtIndex:0];
    
    // 背景图
    [self.rv.segment setBackgroundImage:[UIImage imageNamed:@"101.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    
}

-(void)segmentAction:(UISegmentedControl *)sender{
    //选中的索引分段:selectedSegmentIndex,索引从0开始
    NSLog(@"%ld",sender.selectedSegmentIndex);
    
    switch (sender.selectedSegmentIndex) {
        case 0:
            NSLog(@"first");
            break;
        case 1:
            NSLog(@"second");
            break;
        case 2:
            NSLog(@"three");
            break;
        case 3:
            NSLog(@"four");
            break;
        case 4:
            NSLog(@"five");
            break;
        default:
            break;
    }
    
    
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
   
}


@end

RootView.h

#import <UIKit/UIKit.h>

@interface RootView : UIView

//分段控制器
@property(nonatomic,strong)UISegmentedControl * segment;

@end

RootView.m

#import "RootView.h"

@implementation RootView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self p_setupViews];
    }
    return self;
}


-(void)p_setupViews{
    self.backgroundColor = [UIColor yellowColor];
    
    NSArray * array = @[@"first",@"second",@"third"];
    
    // 创建分段控制器(分段控制器的初始化)
    self.segment = [[UISegmentedControl alloc]initWithItems:array];
    
    self.segment.frame = CGRectMake(50, 50, 200, 50);

    
    // 修改控件颜色
    self.segment.tintColor = [UIColor redColor];
    
    [self addSubview:_segment];
    
}

@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值