标签:
效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 @property (strong, nonatomic) NSArray *arrColorName; 5 6 @end
ViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 - (void)layoutUI; 5 - (void)segmentDidChange:(UISegmentedControl *)sender; 6 @end 7 8 @implementation ViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 13 [self layoutUI]; 14 } 15 16 - (void)didReceiveMemoryWarning { 17 [super didReceiveMemoryWarning]; 18 // Dispose of any resources that can be recreated. 19 } 20 21 - (void)layoutUI { 22 _arrColorName = @[@"blackColor", @"blueColor", @"brownColor"]; 23 UIColor *color = [UIColor colorWithRed:1.000 green:0.473 blue:0.813 alpha:1.000]; 24 self.view.backgroundColor = color; 25 26 UISegmentedControl *smtBackgroundColor = [[UISegmentedControl alloc] initWithItems:_arrColorName]; 27 smtBackgroundColor.frame = CGRectMake(0, 0, 240, 40); 28 smtBackgroundColor.tintColor = color; 29 [smtBackgroundColor addTarget:self 30 action:@selector(segmentDidChange:) 31 forControlEvents:UIControlEventValueChanged]; 32 33 //将分段卡追加到导航条的右侧 34 UIBarButtonItem *barBtnRight = [[UIBarButtonItem alloc] initWithCustomView:smtBackgroundColor]; 35 self.navigationItem.rightBarButtonItem = barBtnRight; 36 } 37 38 - (void)segmentDidChange:(UISegmentedControl *)sender { 39 NSString *colorName = _arrColorName[sender.selectedSegmentIndex]; 40 SEL selColor = NSSelectorFromString(colorName); 41 UIColor *color = [UIColor performSelector:selColor]; 42 self.view.backgroundColor = color; 43 sender.tintColor = color; 44 } 45 46 @end
AppDelegate.h
1 #import <UIKit/UIKit.h> 2 3 @interface AppDelegate : UIResponder <UIApplicationDelegate> 4 @property (strong, nonatomic) UIWindow *window; 5 @property (strong, nonatomic) UINavigationController *navigationController; 6 7 @end
AppDelegate.m
1 #import "AppDelegate.h" 2 #import "ViewController.h" 3 4 @interface AppDelegate () 5 @end 6 7 @implementation AppDelegate 8 9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 10 _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 11 ViewController *viewController = [[ViewController alloc] init]; 12 _navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]; 13 _window.rootViewController = _navigationController; 14 [_window addSubview:_navigationController.view]; 15 [_window makeKeyAndVisible]; 16 return YES; 17 } 18 19 - (void)applicationWillResignActive:(UIApplication *)application { 20 } 21 22 - (void)applicationDidEnterBackground:(UIApplication *)application { 23 } 24 25 - (void)applicationWillEnterForeground:(UIApplication *)application { 26 } 27 28 - (void)applicationDidBecomeActive:(UIApplication *)application { 29 } 30 31 - (void)applicationWillTerminate:(UIApplication *)application { 32 } 33 34 @end
标签:
原文地址:http://www.cnblogs.com/huangjianwu/p/4579141.html