UISegmentedControl的使用

     NSString *strTitle;
     NSMutableArray *myarray = [[NSMutableArray alloc] init];
    //循环取出title
    for (int i = 0; i < _dataFile.count; i++) {
        strTitle = [[_dataFile objectAtIndex:i] objectForKey:@"title"];
        //把循环出来的title循环添加到array里面
        [myarray addObject:strTitle];
    }
    //可变转不可变
    NSArray *array = [NSArray arrayWithArray:myarray];
    
    //把title添加到UISegmentedControl
    UISegmentedControl *segmentedButton = [[UISegmentedControl alloc] initWithItems:array];
    segmentedButton.frame = CGRectMake(10, 8,SWIDTH-20, 50);
    segmentedButton.layer.cornerRadius = 8;
    segmentedButton.backgroundColor = [UIColor colorWithHexString:@"0xA3A3A3"];
    segmentedButton.layer.borderColor = [[UIColor colorWithHexString:@"0xA3A3A3"] CGColor];
    segmentedButton.layer.borderWidth = 1.0;
    segmentedButton.layer.masksToBounds = YES;
    
    //这个是设置按下按钮时的颜色
    segmentedButton.tintColor = [UIColor colorWithHexString:@"0x639AFE"];
    segmentedButton.selectedSegmentIndex = 0;//默认选中的按钮索引
    //正常状态和按下状态的属性控制,比如字体的大小和颜色等
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:12],NSFontAttributeName,[UIColor whiteColor], NSForegroundColorAttributeName, nil];
    [segmentedButton setTitleTextAttributes:attributes forState:UIControlStateNormal];
    segmentedButton.apportionsSegmentWidthsByContent = NO;
    
    //选中时字体颜色
    NSDictionary *selectedAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    [segmentedButton setTitleTextAttributes:selectedAttributes forState:UIControlStateSelected];
    //设置分段控件点击相应事件
    [segmentedButton addTarget:self action:@selector(homeInformation:)forControlEvents:UIControlEventValueChanged];
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, 66)];
    UIImageView *bgImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, 66)];
    [bgImage setImage:[UIImage imageNamed:@"home-bgImage1.png"]];
    [bgView addSubview:bgImage];
    [bgView addSubview:segmentedButton];
    [self.view addSubview:bgView];
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值