码迷,mamicode.com
首页 > 其他好文 > 详细

056在按钮中添加图像

时间:2015-06-15 12:50:11      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:

效果如下:

技术分享

ViewController.h

1 #import <UIKit/UIKit.h>
2 
3 @interface ViewController : UIViewController
4 @property (strong, nonatomic) UIButton *btnShadow;
5 
6 @end

ViewController.m

 1 #import "ViewController.h"
 2 
 3 @interface ViewController ()
 4 - (void)disableStateSwitch:(UISwitch *)sender;
 5 @end
 6 
 7 @implementation ViewController
 8 
 9 - (void)viewDidLoad {
10     [super viewDidLoad];
11     self.view.backgroundColor = [UIColor whiteColor];
12     self.navigationItem.title = @"在按钮中添加图像";
13     UISwitch *swtDisableState = [[UISwitch alloc] init];
14     swtDisableState.on = YES;
15     [swtDisableState addTarget:self action:@selector(disableStateSwitch:) forControlEvents:UIControlEventValueChanged];
16     UIBarButtonItem *barBtnDisableState = [[UIBarButtonItem alloc] initWithCustomView:swtDisableState];
17     [self setToolbarItems:@[barBtnDisableState] animated:YES];
18     
19     _btnShadow = [UIButton buttonWithType:UIButtonTypeCustom];
20     _btnShadow.frame = CGRectMake(0, 0, 260, 60);
21     _btnShadow.center = self.view.center;
22     _btnShadow.backgroundColor = [UIColor whiteColor];
23     _btnShadow.layer.masksToBounds = YES;
24     _btnShadow.layer.cornerRadius = 8.0;
25     //设置按钮标题字体及阴影颜色
26     _btnShadow.titleLabel.font = [UIFont boldSystemFontOfSize:20];
27     _btnShadow.titleLabel.shadowOffset = CGSizeMake(2.0, 2.0);
28     //设置普通状态下的显示特征
29     [_btnShadow setTitle:@"点我进行状态图片切换" forState:UIControlStateNormal];
30     [_btnShadow setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
31     [_btnShadow setTitleShadowColor:[UIColor grayColor] forState:UIControlStateNormal];
32     
33     //添加用于按钮各状态下的图片
34     [_btnShadow setImage:[UIImage imageNamed:@"DogNormal"] forState:UIControlStateNormal];
35     [_btnShadow setImage:[UIImage imageNamed:@"DogHighlighted"] forState:UIControlStateHighlighted];
36     [_btnShadow setImage:[UIImage imageNamed:@"DogDisabled"] forState:UIControlStateDisabled];
37     //设置普通状态下的背景图片
38     UIImage *imgStretchable = [[UIImage imageNamed:@"Frame"] stretchableImageWithLeftCapWidth:20 topCapHeight:20];
39     [_btnShadow setBackgroundImage:imgStretchable forState:UIControlStateNormal];
40     
41     [self.view addSubview:_btnShadow];
42 }
43 
44 - (void)didReceiveMemoryWarning {
45     [super didReceiveMemoryWarning];
46     // Dispose of any resources that can be recreated.
47 }
48 
49 - (void)viewWillAppear:(BOOL)animated {
50     [super viewWillAppear:animated];
51     [self.navigationController setNavigationBarHidden:NO animated:animated];
52     [self.navigationController setToolbarHidden:NO animated:animated];
53 }
54 
55 - (void)disableStateSwitch:(UISwitch *)sender {
56     _btnShadow.enabled = sender.on;
57 }
58 
59 @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

 

056在按钮中添加图像

标签:

原文地址:http://www.cnblogs.com/huangjianwu/p/4576752.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!