ios UISegmentedControl的定制

Posted 其意亦凡

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ios UISegmentedControl的定制相关的知识,希望对你有一定的参考价值。

先看效果图

代码:

@property (nonatomic, strong) UISegmentedControl *segmentedControl;


-(void)createSegMentController{
    NSArray *segmentedArray = [NSArray arrayWithObjects:@"通知",@"评论",nil];
        UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];
    segmentedControl.frame = CGRectMake(0, 0, 190, 22);
    segmentedControl.selectedSegmentIndex = 0;
    segmentedControl.tintColor = UIColorFromRGB(0x3E4759);
    segmentedControl.backgroundColor = [UIColor whiteColor];
    segmentedControl.layer.masksToBounds = YES;
    segmentedControl.layer.cornerRadius = 12;
    segmentedControl.layer.borderWidth = 0.5;
    segmentedControl.layer.borderColor = UIColorFromRGB(0x3E4759).CGColor;
    NSDictionary *dicnor = [NSDictionary dictionaryWithObjectsAndKeys:UIColorFromRGB(0x3E4759),NSForegroundColorAttributeName,[UIFont systemFontOfSize:14],NSFontAttributeName, nil];
    [segmentedControl setTitleTextAttributes:dicnor forState:UIControlStateNormal];
    NSDictionary *dicselect = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,[UIFont systemFontOfSize:14],NSFontAttributeName, nil];
    [segmentedControl setTitleTextAttributes:dicselect forState:UIControlStateSelected];

    [segmentedControl addTarget:self action:@selector(indexDidChangeForSegmentedControl:) forControlEvents:UIControlEventValueChanged];
    [self.navigationItem setTitleView:segmentedControl];
    self.segmentedControl = segmentedControl;

}

-(void)indexDidChangeForSegmentedControl:(UISegmentedControl *)sender
{
    NSInteger selecIndex = sender.selectedSegmentIndex;
    if (selecIndex == 0) {
        sender.selectedSegmentIndex = 0;
       
    } else {
        sender.selectedSegmentIndex = 1;
       
    }
}

 

以上是关于ios UISegmentedControl的定制的主要内容,如果未能解决你的问题,请参考以下文章

iOS:改变 UISegmentedcontrol 的高度

iOS - 这是由 UISegmentedControl 控制的 UIPageViewController,还是这是啥?

iOS学习七之UISegmentedControl

ios UISegmentedControl的使用简介

升级到 iOS 6 后 UISegmentedControl 的显示问题

iOS 7 中 UINavigationbar 下方的 UISegmentedControl