UISegmentedControl 踩坑

时间:2022-04-17 09:07:32

@interface JLMyContactsViewController ()

@property (nonatomic, strong)   UIImageView *navImageView;

@property (nonatomic, strong)   UISegmentedControl *titleV;

@property (nonatomic, strong)   NSMutableArray *titleVImages;

@property (nonatomic, strong)   NSArray *titleVImageNames;

@end

- (void)setUpUI{

self.titleVImageNames = @[@"我的人脉点击11_03",@"我的人脉未点击11_03",@"时间轴点击_03",@"时间轴未点击_03"];

//    self.titleVImages = [NSMutableArray array];

[self.titleVImageNames enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

//

UIImage *image = [[UIImage imageNamed:(NSString *)obj] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

UIImage *newImage = [[UIImage scaleToSize:image size:CGSizeMake( JLAppWidth/9*5/2, JLAppHeight/20 - 10)] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

[self.titleVImages addObject:newImage];

}];

[self.titleV setImage:self.titleVImages[0] forSegmentAtIndex:0];

[self.titleV setImage:self.titleVImages[3] forSegmentAtIndex:1];

self.titleV.selectedSegmentIndex = 0;

[self.titleV addTarget:self action:@selector(titleVChange:) forControlEvents:UIControlEventValueChanged];

[self.view addSubview:self.titleV];

}

#pragma mark --PrivateMethod

- (void)titleVChange:(UISegmentedControl *)sender{

sender.selected = !sender.selected;

[self.titleV setImage:sender.selected ?self.titleVImages[1]:self.titleVImages[0] forSegmentAtIndex:0];

[self.titleV setImage:sender.selected ? self.titleVImages[2]:self.titleVImages[3] forSegmentAtIndex:1];

if (sender.selectedSegmentIndex == 0) {

//1.添加tableView 2.移除时间轴

}else{

//相反

}

}

#pragma mark --Getter

-(UIImageView *)navImageView{

if (!_navImageView) {

_navImageView = [[UIImageView alloc] init];

_navImageView.frame = CGRectMake(0, 0, JLAppWidth/5 + 10, 25);

}

return _navImageView;

}

- (UISegmentedControl *)titleV{

if (!_titleV) {

_titleV = [[UISegmentedControl alloc] initWithItems:@[self.titleVImages[0] ,self.titleVImages[3]]];

_titleV.frame = CGRectMake(JLAppWidth/8 *3 - 60, 10, JLAppWidth/9*5, JLAppHeight/20 - 10);

_titleV.tintColor = [UIColor colorWithRed:0.8196 green:0.8196 blue:0.8196 alpha:1.0];

}

return _titleV;

}