UI - UISegmentedControl

<AppDelegate.m>

#import "AppDelegate.h"
#import "RootViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    RootViewController *rootVC = [[RootViewController alloc ]init ];
    self.window.rootViewController = rootVC;
    [rootVC release];
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end


<RootViewController.h>

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end


<RootViewController.m>

#import "RootViewController.h"

@interface RootViewController ()
@property (nonatomic,retain)UIView *redView;
@property (nonatomic,retain)UIView *blueView;
@property (nonatomic,retain)UIView *greenView;
@end

@implementation RootViewController

-(void)dealloc
{
    self.redView = nil;
    self.blueView = nil;
    self.greenView = nil;
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //布局 segmentControl
    [self layoutSegementControl];
    
    //布局 views
    [self layoutViews];

}
//布局 segmentControl
-(void)layoutSegementControl
{
    //1.创建对象
    UISegmentedControl *segmentControl = [[UISegmentedControl alloc] initWithItems:@[@"红色",@"绿色",@"蓝色"]];  //数组的语法糖
    //2.配置属性
    //设置中心点
    segmentControl.center = CGPointMake(160, 50);
    //背景颜色
    segmentControl.backgroundColor = [UIColor yellowColor];
    //渲染颜色
    segmentControl.tintColor = [UIColor purpleColor];
    //设置默认选择的 item     不设置的话,不显示任何一个
    segmentControl.selectedSegmentIndex = 1;
    //获取总选项数(只读)
    NSLog(@"%ld", segmentControl.numberOfSegments);
    //在点击后是否恢复原样,默认为NO
    segmentControl.momentary = NO;
    //设置指定索引选项的宽度
    [segmentControl setWidth:70 forSegmentAtIndex:1];
    //设置选项卡内部文字或者图片与默认位置的偏移量,默认位置在选项卡的中心。
    [segmentControl setContentOffset:CGSizeMake(10, 0) forSegmentAtIndex:1];
    //设置指定索引选项不可选
    [segmentControl setEnabled:NO forSegmentAtIndex:2];
    
    //设置指定索引的title
//    [segmentControl setTitle:@"红色" forSegmentAtIndex:2];
//    [segmentControl setTitle:@"蓝色" forSegmentAtIndex:0];
    //在某个位置插入 item
//    [segmentControl insertSegmentWithTitle:@"黄色" atIndex:1 animated:YES];
    
    //设置指定索引的图片
//    UIImage *pic = [[UIImage imageNamed:@"qq.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    [segmentControl setImage:pic forSegmentAtIndex:2];
    //在指定索引插入一个选项并设置图片
//    [segmentControl insertSegmentWithImage:pic atIndex:0 animated:YES];
    /*
    UIImageRenderingMode枚举中包含下列值:
    UIImageRenderingModeAutomatic        // 根据图片的使用环境和所处的绘图上下文自动调整渲染模式。
    UIImageRenderingModeAlwaysOriginal   // 始终绘制图片原始状态,不使用Tint Color。
    UIImageRenderingModeAlwaysTemplate   // 始终根据Tint Color绘制图片,忽略图片的颜色信息。
    renderingMode属性的默认值是UIImageRenderingModeAutomatic,即UIImage是否使用Tint Color取决于它显示的位置。
     着色(Tint Color)是iOS7界面中的一个.设置UIImage的渲染模式:UIImage.renderingMode重大改变,你可以设置一个UIImage在渲染时是否使用当前视图的Tint Color。
     所以在添加图片的时候,会不显示图片,只显示 segmentControl 的 tint color, 故需要将其着色改为原始状态
     */

    //移除指定索引的选项
//    [segmentControl removeSegmentAtIndex:0 animated:YES];
    //移除所有选项
//    [segmentControl removeAllSegments];
    //添加点击事件
    [segmentControl addTarget:self action:@selector(changeView:) forControlEvents:UIControlEventValueChanged];
    
    //3.添加至父视图
    [self.view addSubview:segmentControl];
    //4.释放
    [segmentControl release];
}

//布局 views
-(void)layoutViews
{
    self.redView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 320, 468)];
    _redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_redView];
    [_redView release];
    
    self.blueView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 320, 468)];
    _blueView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:_blueView];
    [_blueView release];
    
    self.greenView = [[UIView alloc ]initWithFrame:CGRectMake(0, 100, 320, 468)];
    _greenView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:_greenView];
    [_greenView release];
    
    
}

-(void)changeView:(UISegmentedControl *)segment
{
    NSLog(@"当前点击的是第%d个item",segment.selectedSegmentIndex);
    //根据 segmentContro 的下标来变换视图层
    if (segment.selectedSegmentIndex == 0 )
    {
        [self.view bringSubviewToFront:_blueView];
    } else if (segment.selectedSegmentIndex == 1)
    {
        [self.view bringSubviewToFront:_greenView];
    } else if(segment.selectedSegmentIndex == 2)
    {
        [self.view bringSubviewToFront:_redView];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

你可能感兴趣的:(UI - UISegmentedControl)