分段控件 (UISegmentedControl)

一. 分段控件 (UISegmentedControl) 

控件展示 : 

1. UISegmentedControl 控件属性

  

(1) Style 属性

Style 属性 

     

-- Plain : 分段控件使用最普通的风格;

-- Bordered : 在最普通风格上添加一圈边框;

-- Bar : 分段控件使用工具条风格;

(2) State 属性

State 属性 : 

-- Momentary 复选框 : 勾选复选框后, 分段控件不保存控件状态, 如果勾选后, 点击时高亮, 点击后恢复原样;

(3) Tint 属性

Tint 属性 : 

-- 作用 : 设置分段控件被选中的高亮颜色;

-- 效果展示 : 

(4) Segments 属性

Segments 属性 : 

-- 作用 : 控制分成几段;

-- 展示效果 : 

 

(5) Segment 属性

Segment 属性 : 

      

-- 作用 : 为不同的分段设置对应的 标题, 图片 等内容;

(6) Tittle 属性

Tittle 属性 : 每个 Segment 都有一个 Tittle 属性, 就是分段按钮每个按钮的标题;

(7) Image 属性

Image 属性 : 为不同的 分段 Segment 设置图片;

(8) Behavior 属性

Behavior 属性 : 

-- Enable 复选框 : 用于设置 Segment 是否可用;

-- Selected 复选框 : 用于设置 Segment 是否被选中;

2. 使用 UISegmentedControl 改变背景颜色

(1) 设置 UISegmentedControl 属性

 

UISegmentedControl 属性 : 

-- 属性截图 : 

(2) 设置 UISegmentedControl 响应方法

创建 UISegmentedControl 的 IBAction : 

-- 按住 control 键将 UISegmentedControl 拖动到 OCViewController.h 中 : 

-- 设置 IBAction 属性 : 

-- 方法代码 : 

 

[objc]  view plain copy
 
  1. - (IBAction)segmentControl:(id)sender {  
  2.     int index = [sender selectedSegmentIndex];  
  3.       
  4.     switch (index) {  
  5.         case 0:  
  6.             self.baseView.backgroundColor = [UIColor whiteColor];  
  7.             break;  
  8.         case 1:  
  9.             self.baseView.backgroundColor = [UIColor blackColor];  
  10.             break;  
  11.         case 2:  
  12.             self.view.backgroundColor = [UIColor greenColor];  
  13.             break;  
  14.         case 3:  
  15.             self.view.backgroundColor = [UIColor blueColor];  
  16.             break;  
  17.               
  18.         default:  
  19.             break;  
  20.     }  
  21. }  

(3) 代码示例

代码示例 : 

-- OCViewController.h : 

[objc]  view plain copy
 
  1. //  
  2. //  OCViewController.h  
  3. //  UISegmentedControl  
  4. //  
  5. //  Created by octopus on 15-12-4.  
  6. //  Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface OCViewController : UIViewController  
  12. //背景 UIView  
  13. @property (strong, nonatomic) IBOutlet UIView *baseView;  
  14. - (IBAction)segmentControl:(id)sender;  
  15.   
  16. @end  

-- OCViewController.m : 

[objc]  view plain copy
 
  1. //  
  2. //  OCViewController.m  
  3. //  UISegmentedControl  
  4. //  
  5. //  Created by octopus on 15-12-4.  
  6. //  Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.  
  7. //  
  8.   
  9. #import "OCViewController.h"  
  10.   
  11. @interface OCViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation OCViewController  
  16.   
  17. - (void)viewDidLoad  
  18. {  
  19.     [super viewDidLoad];  
  20.     // Do any additional setup after loading the view, typically from a nib.  
  21. }  
  22.   
  23. - (void)didReceiveMemoryWarning  
  24. {  
  25.     [super didReceiveMemoryWarning];  
  26.     // Dispose of any resources that can be recreated.  
  27. }  
  28.   
  29. - (IBAction)segmentControl:(id)sender {  
  30.     int index = [sender selectedSegmentIndex];  
  31.       
  32.     switch (index) {  
  33.         case 0:  
  34.             self.baseView.backgroundColor = [UIColor whiteColor];  
  35.             break;  
  36.         case 1:  
  37.             self.baseView.backgroundColor = [UIColor blackColor];  
  38.             break;  
  39.         case 2:  
  40.             self.view.backgroundColor = [UIColor greenColor];  
  41.             break;  
  42.         case 3:  
  43.             self.view.backgroundColor = [UIColor blueColor];  
  44.             break;  
  45.               
  46.         default:  
  47.             break;  
  48.     }  
  49. }  
  50. @end  

-- 界面展示 : 

3. 动态增加删除分段

(1) 主要 API 简介

插入 删除分段 : 

-- 插入分段 : 调用 segmentControl 的 insertSegmentWithTittle 方法, 参数一 标题, 参数二 插入索引;

[objc]  view plain copy
  1. [self.segmentControl insertSegmentWithTitle:tittle atIndex:count animated:YES];  

-- 删除分段 : 删除只需注明 索引值 即可;

[objc]  view plain copy
 
  1. [self.segmentControl removeSegmentAtIndex:count - 1 animated:YES];  

(2) 源码示例

源码示例 

-- 界面设计文件 : 

-- OCViewController.h : 

[objc]  view plain copy
 
  1. //  
  2. //  OCViewController.h  
  3. //  UISegmentedControl  
  4. //  
  5. //  Created by octopus on 15-12-4.  
  6. //  Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface OCViewController : UIViewController  
  12. //背景 UIView  
  13. @property (strong, nonatomic) IBOutlet UIView *baseView;  
  14. //分段控件  
  15. @property (strong, nonatomic) IBOutlet UISegmentedControl *segmentControl;  
  16. //单行文本  
  17. @property (strong, nonatomic) IBOutlet UITextField *textField;  
  18.   
  19. //分段控件方法  
  20. - (IBAction)segmentControl:(id)sender;  
  21. //点击背景控件方法  
  22. - (IBAction)clickBackGround:(id)sender;  
  23.   
  24. //添加分段控件  
  25. - (IBAction)addSegment:(id)sender;  
  26. //删除分段控件  
  27. - (IBAction)minusSegment:(id)sender;  
  28.   
  29.   
  30. @end  

-- OCViewController.m : 

[objc]  view plain copy
 
  1. //  
  2. //  OCViewController.m  
  3. //  UISegmentedControl  
  4. //  
  5. //  Created by octopus on 15-12-4.  
  6. //  Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.  
  7. //  
  8.   
  9. #import "OCViewController.h"  
  10.   
  11. @interface OCViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation OCViewController  
  16.   
  17. - (void)viewDidLoad  
  18. {  
  19.     [super viewDidLoad];  
  20.     // Do any additional setup after loading the view, typically from a nib.  
  21. }  
  22.   
  23. - (void)didReceiveMemoryWarning  
  24. {  
  25.     [super didReceiveMemoryWarning];  
  26.     // Dispose of any resources that can be recreated.  
  27. }  
  28.   
  29. //分段控件响应方法  
  30. - (IBAction)segmentControl:(id)sender {  
  31.     int index = [sender selectedSegmentIndex];  
  32.       
  33.     switch (index) {  
  34.         case 0:  
  35.             self.baseView.backgroundColor = [UIColor whiteColor];  
  36.             break;  
  37.         case 1:  
  38.             self.baseView.backgroundColor = [UIColor blackColor];  
  39.             break;  
  40.         case 2:  
  41.             self.view.backgroundColor = [UIColor greenColor];  
  42.             break;  
  43.         case 3:  
  44.             self.view.backgroundColor = [UIColor blueColor];  
  45.             break;  
  46.               
  47.         default:  
  48.             break;  
  49.     }  
  50. }  
  51.   
  52. - (IBAction)clickBackGround:(id)sender {  
  53.     // 点击背景 取消虚拟键盘  
  54.     [self.textField resignFirstResponder];  
  55. }  
  56.   
  57. //添加分段控件  
  58. - (IBAction)addSegment:(id)sender {  
  59.     NSUInteger count = self.segmentControl.numberOfSegments;  
  60.       
  61.     NSString * tittle = self.textField.text;  
  62.     if ([tittle length] > 0) {  
  63.         [self.segmentControl insertSegmentWithTitle:tittle atIndex:count animated:YES];  
  64.         self.textField.text = @"";  
  65.     }  
  66. }  
  67.   
  68. //删除分段控件  
  69. - (IBAction)minusSegment:(id)sender {  
  70.     NSUInteger count = self.segmentControl.numberOfSegments;  
  71.     [self.segmentControl removeSegmentAtIndex:count - 1 animated:YES];  
  72. }  
  73. @end  

-- 界面展示 : 

 

转载于:https://www.cnblogs.com/sungk/p/5108781.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值