美文网首页
UISegmentControl

UISegmentControl

作者: 霜之幽语 | 来源:发表于2018-01-30 11:33 被阅读2次
    - (void)init {
        NSArray *segmentedArray = [NSArrayarrayWithObjects:STR(@"Mynews"),STR(@"Systemmessage"),nil];
    
        UISegmentedControl *segmentedControl = [[UISegmentedControlalloc]initWithItems:segmentedArray];
    
        segmentedControl.backgroundColor = [UIColorkvColorWithRed:21green:33blue:46alpha:1];
    
        segmentedControl.layer.masksToBounds = YES;               //    默认为no,不设置则下面一句无效
    
        segmentedControl.layer.cornerRadius = 8;               //    设置圆角大小,同UIView
    
        segmentedControl.layer.borderWidth = 1.5;                   //    边框宽度,重新画边框,若不重新画,可能会出现圆角处无边框的情况
    
        segmentedControl.layer.borderColor = [UIColorblackColor].CGColor; //     边框颜色
    
        segmentedControl.frame = CGRectMake(0, 0.15029*CFG, 0.2716*CFW, 0.0814*CFG); // 0.3642*CFW
    
        segmentedControl.selectedSegmentIndex = 0;
    
        segmentedControl.tintColor = [UIColorkvColorWithRed:252green:108blue:33alpha:1];
    
    //    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    
        [segmentedControl setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColorwhiteColor]}forState:UIControlStateSelected];
    
        [segmentedControl setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColorkvColorWithRed:129green:129blue:129alpha:1],NSFontAttributeName:[UIFontboldSystemFontOfSize:14.0f]}forState:UIControlStateNormal];
    
    //    UIFont *font = [UIFont boldSystemFontOfSize:14.0f];   // 设置字体大小
    
    //    NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
    
    //                                                           forKey:NSFontAttributeName];
    
    //    [segmentedControl setTitleTextAttributes:attributes
    
    //                                    forState:UIControlStateNormal];
    
        [self.viewaddSubview:segmentedControl];
    
        //添加事件
        [segmentedControl addTarget:selfaction:@selector(change:) forControlEvents:UIControlEventValueChanged]
    
    }
    
    
    //点击不同分段就会有不同的事件进行相应
    
    - (void)change:(UISegmentedControl *)sender {
    
        NSLog(@"测试");
    
        if (sender.selectedSegmentIndex == 0) {
    
            NSLog(@"1");
    
            [self.systemNewsViewremoveFromSuperview];
    
            [self.viewaddSubview:self.myTableView];
    
            [self.myTableViewreloadData];
    
        } elseif (sender.selectedSegmentIndex == 1) {
    
            NSLog(@"2");
    
            [self.viewaddSubview:[selfsystemNewsView]];
    
            [self.myTableViewremoveFromSuperview];
    
            [self.systemNewsViewreloadData];
    
        }
    }
    

    相关文章

      网友评论

          本文标题:UISegmentControl

          本文链接:https://www.haomeiwen.com/subject/qsoqzxtx.html