效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 @property (strong, nonatomic) UIButton *btnState; 5 6 @end
ViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 5 - (void)disableStateSwitch:(UISwitch *)sender; 6 @end 7 8 @implementation ViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 self.view.backgroundColor = [UIColor whiteColor]; 13 self.navigationItem.title = @"点击按钮后改变按钮的文字"; 14 UISwitch *swtDisableState = [[UISwitch alloc] init]; 15 swtDisableState.on = YES; 16 [swtDisableState addTarget:self action:@selector(disableStateSwitch:) forControlEvents:UIControlEventValueChanged]; 17 UIBarButtonItem *barBtnDisableState = [[UIBarButtonItem alloc] initWithCustomView:swtDisableState]; 18 [self setToolbarItems:@[barBtnDisableState] animated:YES]; 19 20 _btnState = [UIButton buttonWithType:UIButtonTypeCustom]; 21 _btnState.frame = CGRectMake(0, 0, 200, 60); 22 _btnState.center = self.view.center; 23 _btnState.backgroundColor = [UIColor colorWithRed:0.513 green:0.894 blue:1.000 alpha:1.000]; 24 _btnState.layer.masksToBounds = YES; 25 _btnState.layer.cornerRadius = 10.0; 26 _btnState.layer.borderColor = [UIColor colorWithRed:1.000 green:0.572 blue:0.894 alpha:1.000].CGColor; 27 _btnState.layer.borderWidth = 2.0; 28 //设置按钮标题字体及阴影颜色 29 _btnState.titleLabel.font = [UIFont boldSystemFontOfSize:22]; 30 _btnState.titleLabel.shadowOffset = CGSizeMake(2.0, 2.0); 31 //设置普通状态下的显示特征 32 [_btnState setTitle:@"点我" forState:UIControlStateNormal]; 33 [_btnState setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 34 [_btnState setTitleShadowColor:[UIColor grayColor] forState:UIControlStateNormal]; 35 //设置按钮被触摸时,即高亮状态下的显示特征 36 [_btnState setTitle:@"变样子了" forState:UIControlStateHighlighted]; 37 [_btnState setTitleColor:[UIColor brownColor] forState:UIControlStateHighlighted]; 38 [_btnState setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateHighlighted]; 39 //设置按钮非活性(无效)状态下的显示特征 40 [_btnState setTitle:@"禁用按钮了" forState:UIControlStateDisabled]; 41 [_btnState setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled]; 42 [_btnState setTitleShadowColor:[UIColor blackColor] forState:UIControlStateDisabled]; 43 [self.view addSubview:_btnState]; 44 } 45 46 - (void)didReceiveMemoryWarning { 47 [super didReceiveMemoryWarning]; 48 // Dispose of any resources that can be recreated. 49 } 50 51 - (void)viewWillAppear:(BOOL)animated { 52 [super viewWillAppear:animated]; 53 [self.navigationController setNavigationBarHidden:NO animated:animated]; 54 [self.navigationController setToolbarHidden:NO animated:animated]; 55 } 56 57 - (void)disableStateSwitch:(UISwitch *)sender { 58 _btnState.enabled = sender.on; 59 } 60 61 @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
时间: 2024-11-01 04:40:19