IOS-自定義UISegmentedControl

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章