UISegmentedControl 分段控件

#import "AppDelegate.h"

#import "RootViewController.h"

@interface AppDelegate ()

 

@end

 

@implementation AppDelegate

 

- (void)dealloc

{

    [_window release];

    [super dealloc];

}

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    RootViewController *rootVC = [[RootViewController alloc]init];

    self.window.rootViewController = rootVC;

    [rootVC release];

    [self.window makeKeyAndVisible];

    return YES;

}

 

 

 

 

 

#import "RootViewController.h"

 

@interface RootViewController ()

 

@end

 

@implementation RootViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    //创建一个分段控件

    

    

    UISegmentedControl *rootView1 = [[UISegmentedControl alloc]initWithItems:@[@"全部商家",@"优惠商家",@"我的"]];

    rootView1.center = CGPointMake(180, 100);

    // 修改 文字的颜色为橘色

    rootView1.tintColor = [UIColor orangeColor];

//    [rootView1 setTintColor:[UIColor orangeColor]];

//    rootView1.backgroundColor = [UIColor orangeColor];

    // 每个 rootView1 的大小默认是平分整个 rootView1 的大小,如果把 apportionsSegmentWidthsByContent 设置为 YES,会根据内容来分配每一个 rootView1 的大小。

    rootView1.apportionsSegmentWidthsByContent = YES;

    

    // 设置当前 rootView1 为选中的下标

    rootView1.selectedSegmentIndex = 1;

    

    // 设置点击时处于阴影状态,点击后恢复原状

//    rootView1.momentary = YES;

    

    // rootView1 添加事件

    [rootView1 addTarget:self action:@selector(shiJian1:) forControlEvents:UIControlEventValueChanged];

    

    [self.view addSubview:rootView1];

    [rootView1 release];

    

}

 

- (void)shiJian1:(UISegmentedControl *)shijian1

{

    NSLog(@"添加事件成功!");

    // 根据当前选中的 rootView1 的下标,修改 self.view 的背景颜色。(0----->红色,1--->黄色,2---->蓝色)

    // 方法一:

    switch (shijian1.selectedSegmentIndex) {

        case 0:

            self.view.backgroundColor = [UIColor redColor];

            shijian1.tintColor = [UIColor blackColor];

            break;

        case 1:

            self.view.backgroundColor = [UIColor yellowColor];

            shijian1.tintColor = [UIColor magentaColor];

            break;

        case 2:

            self.view.backgroundColor = [UIColor blueColor];

            shijian1.tintColor = [UIColor greenColor];

            break;

        default:

            break;

    }

    // 方法二:

//    if (shijian1.selectedSegmentIndex == 0)

//    {

//        self.view.backgroundColor = [UIColor magentaColor];

//    }else if(shijian1.selectedSegmentIndex == 1)

//    {

//        self.view.backgroundColor = [UIColor yellowColor];

//    }else if(shijian1.selectedSegmentIndex == 2)

//    {

//        self.view.backgroundColor = [UIColor purpleColor];

//    }

 

}

 

 

 

原文地址:https://www.cnblogs.com/jx451578429/p/4760122.html