现在的位置: 首页 > 综合 > 正文

IOS-自定义UISegmentedControl

2018年02月02日 ⁄ 综合 ⁄ 共 1665字 ⁄ 字号 评论关闭

#define IOS7_OR_LATER ([[[UIDevice currentDevice] systemVersion] compare:@"7.0"] != NSOrderedAscending)

    

NSArray *array = [[NSArrayalloc]initWithObjects:@"我的动态",@"好友圈",@"好友列表",nil];

   

UISegmentedControl *segment = [[UISegmentedControlalloc]initWithItems:array];

    

    segment.frame =CGRectMake(0,0,320,
40);

    segment.momentary =NO;

    segment.segmentedControlStyle =UISegmentedControlStyleBar;

    [segment setTintColor:[UIColorblackColor]];
//设置segments的颜色

    

    [segment setBackgroundImage:[UIImageimageNamed:@"btnCell_Normal.png"]forState:UIControlStateNormalbarMetrics:UIBarMetricsDefault];

    

    [segment setBackgroundImage:[UIImageimageNamed:@"book_Active.png"]forState:UIControlStateSelectedbarMetrics:UIBarMetricsDefault];

    

    if (IOS7_OR_LATER) {

        [segment
setTitleTextAttributes:[NSDictionarydictionaryWithObject:[UIColorcolorWithRed:189.0f/255.0fgreen:145.0f/255.0fblue:12.0f/255.0falpha:1]
forKey:NSForegroundColorAttributeName]forState:UIControlStateNormal];

        

        [segment
setTitleTextAttributes:[NSDictionarydictionaryWithObject:[UIColorwhiteColor]
forKey:NSForegroundColorAttributeName]forState:UIControlStateSelected];

    }

    else{

        [segment
setTitleTextAttributes:[NSDictionarydictionaryWithObjects:@[[UIColorcolorWithRed:189.0f/255.0fgreen:145.0f/255.0fblue:12.0f/255.0falpha:1.0f],[UIColorclearColor]]forKeys:@[UITextAttributeTextColor,UITextAttributeTextShadowColor]]forState:UIControlStateNormal];

        [segment
setTitleTextAttributes:[NSDictionarydictionaryWithObject:[UIColorwhiteColor]
forKey:UITextAttributeTextColor]forState:UIControlStateSelected];

    }

   [segment
addTarget:self action:@selector(change:) forControlEvents:UIControlEventValueChanged];

    [self.viewaddSubview:segment];

抱歉!评论已关闭.