• 分段控件 (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 plaincopy
     
    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 plaincopy
     
    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 plaincopy
     
    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 方法, 参数一 标题, 参数二 插入索引;

    1. [self.segmentControl insertSegmentWithTitle:tittle atIndex:count animated:YES];  

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

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

    (2) 源码示例

    源码示例 

    -- 界面设计文件 : 

    -- OCViewController.h : 

    [objc] view plaincopy
     
    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 plaincopy
     
    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  

    -- 界面展示 : 

  • 相关阅读:
    从应用到平台,云服务架构的演进过程
    从应用到平台,云服务架构的演进过程
    武汉小猫科技-工作总结(1):一图胜万言
    武汉小猫科技-工作总结(1):一图胜万言
    一个App带你学会Retrofit2.0,麻麻再也不用担心我的网络请求了!
    关于创业公司产品开发原则
    关于创业公司产品开发原则
    【Deep learning book】
    【NLP】word2vec
    【python】好书
  • 原文地址:https://www.cnblogs.com/sungk/p/5108781.html
Copyright © 2020-2023  润新知