• iOS-分段控制器-基本概念


    可以直接复制使用

    #import "FirstViewController.h"
    #import "Masonry.h"
    
    @interface FirstViewController ()
    
    @property (nonatomic, strong) UISegmentedControl * segmentedControl_one;
    
    @end
    
    @implementation FirstViewController
    
    #pragma mark - 生命周期
    #pragma mark viewDidLoad
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        [self basicSetting];
        
        [self addSegmentedControl_one];
        
        [self insertOrDeleteInSegmendtedControl];
        
        [self settingSegmentProperty];
        
        [self obtainSectionItem];
    }
    
    
    #pragma mark - 系统代理
    
    #pragma mark - 点击事件
    - (void)segmentedControl_one:(UISegmentedControl *)sender
    {
        NSLog(@"index: %ld",(long)sender.selectedSegmentIndex);
        
    }
    
    #pragma mark - 实现方法
    #pragma mark 基本设置
    - (void)basicSetting
    {
        self.title = @"基本概念";
    }
    
    - (void)addSegmentedControl_one
    {
        [self.view addSubview:self.segmentedControl_one];
        [self.segmentedControl_one mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(self.view).with.offset(10);
            make.right.mas_equalTo(self.view).with.offset(-10);
            make.top.mas_equalTo(self.view).with.offset(30);
            make.height.mas_equalTo(40);
        }];
    }
    
    - (void)insertOrDeleteInSegmendtedControl
    {
        // 插入一段文字
        [self.segmentedControl_one insertSegmentWithTitle:@"插入的元素" atIndex:2 animated:YES];
        
        // 插入图片
        [self.segmentedControl_one insertSegmentWithImage:[[UIImage imageNamed:@"1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] atIndex:3 animated:YES];
        
        // 删除
        [self.segmentedControl_one removeSegmentAtIndex:0 animated:YES];
        
        // 根据下标修改分段标题
        [self.segmentedControl_one setTitle:@"修改的分段标题" forSegmentAtIndex:4];
    }
    
    - (void)settingSegmentProperty
    {
        // 设置段落的宽度
        [self.segmentedControl_one setWidth:40.0f forSegmentAtIndex:2];
        
        // 设置分段中标题的位置 (0,0)是中心点的位置
        [self.segmentedControl_one setContentOffset:CGSizeMake(10, 10) forSegmentAtIndex:3];
    }
    
    - (void)obtainSectionItem
    {
        //
        //获取指定索引选项的图片  imageForSegmentAtIndex:
        UIImageView *imageForSegmentAtIndex = [[UIImageView alloc]initWithImage:[self.segmentedControl_one imageForSegmentAtIndex:2]];
        imageForSegmentAtIndex.frame =CGRectMake(60.0,100.0,30.0,30.0);
        
        //获取指定索引选项的标题  titleForSegmentAtIndex
        UILabel *titleForSegmentAtIndex = [[UILabel alloc]initWithFrame:CGRectMake(60.0,200.0,200.0,30.0)];
        titleForSegmentAtIndex.text = [self.segmentedControl_one titleForSegmentAtIndex:0];
        
        //获取总选项数   segmentedControl.numberOfSegments
        UILabel *numberOfSegments = [[UILabel alloc]initWithFrame:CGRectMake(60.0,300.0,30.0,200.0)];
        numberOfSegments.text = [NSString stringWithFormat:@"%lu",(unsigned long)self.segmentedControl_one.numberOfSegments];
        
        //获取指定索引选项的宽度  widthForSegmentAtIndex:
        UILabel *widthForSegmentAtIndex = [[UILabel alloc] initWithFrame:CGRectMake(40.0,400.0,200.0,30.0)];
        widthForSegmentAtIndex.text = [NSString stringWithFormat:@"宽度: %f",[self.segmentedControl_one widthForSegmentAtIndex:2]];
        
        //设置默认选择项索引
        self.segmentedControl_one.selectedSegmentIndex =2;
        // 设置色彩
        self.segmentedControl_one.tintColor = [UIColor redColor];
        
        self.segmentedControl_one.momentary = NO;//设置在点击后是否恢复原样
        
        [self.segmentedControl_one setEnabled:NO forSegmentAtIndex:4];//设置指定索引选项不可选
        BOOL enableFlag = [self.segmentedControl_one isEnabledForSegmentAtIndex:4];//判断指定索引选项是否可选
        NSLog(@"enableFlag: %d",enableFlag);
        
        [self.view addSubview:widthForSegmentAtIndex];
        [self.view addSubview:numberOfSegments];
        [self.view addSubview:titleForSegmentAtIndex];
        [self.view addSubview:imageForSegmentAtIndex];
        [self.view addSubview:self.segmentedControl_one];
    }
    
    #pragma mark - setter & getter
    
    - (UISegmentedControl *)segmentedControl_one
    {
        if (!_segmentedControl_one)
        {
            NSArray * array = @[@"第一段",@"第二段",@"第三段",@"第四段"];
            self.segmentedControl_one = [[UISegmentedControl alloc] initWithItems:array];
            
            [self.segmentedControl_one addTarget:self action:@selector(segmentedControl_one:) forControlEvents:UIControlEventValueChanged];
        }
        return _segmentedControl_one;
    }
    
    @end
    你的一次推荐就是对我莫大的支持。感觉不错,给个推荐或者评论吧。
  • 相关阅读:
    黑马程序员JAVA高级视频_IO输入与输出18天2(FileWriter)
    黑马程序员JavaAPI17天5(集合转成数组)
    黑马程序员JAVA高级视频_IO输入与输出19天4(MyBufferedReader)
    android 取消webview的背景色
    DLNA
    Android JNI的若干问题总结
    gcc 一些应用
    如何基于nand flash启动Linux内核(分享一段实用、简单、类似bootloader功能的代码)
    Android JNI开发高级篇有关Android JNI开发中比较强大和有用的功能就是从JNI层创建、构造Java的类或执行Java层的方法获取属性等操作。 一、类的相关操作 1. jclass FindClass(JNIEnv *env, const char *name);
    ubuntu 配置ndk
  • 原文地址:https://www.cnblogs.com/mancong/p/5595502.html
Copyright © 2020-2023  润新知