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];
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];

894

被折叠的 条评论
为什么被折叠?



