自定义导航栏按钮

首先,写一个类,继承于UIButton。

 

头文件:

Ios代码  
#import <Foundation/Foundation.h>  
  
@interface BlueButton : UIButton {  
      
}  
  
- (id)init;  
  
@end  
 1 实现文件:
 2  
 3 Ios代码  
 4 #import "BlueButton.h"  
 5   
 6 @implementation BlueButton  
 7   
 8 - (id)init {  
 9   if(self = [super init]) {  
10     self.frame = CGRectMake(0, 0, 49.0, 30.0);  
11     self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;  
12     self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;  
13           
14     UIImage *image = [UIImage imageNamed:@"blueButton.jpg"];  
15     UIImage *stretchImage = [image stretchableImageWithLeftCapWidth:15.0 topCapHeight:0.0];  
16     [self setBackgroundImage:stretchImage forState:UIControlStateNormal];  
17           
18     self.backgroundColor = [UIColor clearColor];  
19     [self setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];  
20     self.titleShadowOffset = CGSizeMake(0, -1);  
21     self.titleLabel.font = [UIFont boldSystemFontOfSize:13];  
22   }  
23       
24   return self;  
25 }  
26   
27 @end  
28  
29 示例:
30  
31 Ios代码  
32 - (void)viewDidLoad {  
33     [super viewDidLoad];  
34       
35     BlueButton *blueButton = [[BlueButton alloc] init];   
36     [blueButton setTitle:@"Add" forState:UIControlStateNormal];   
37     [blueButton addTarget:self action:@selector(add)  
38              forControlEvents:UIControlEventTouchUpInside];  
39       
40     UIBarButtonItem *button = [[UIBarButtonItem alloc]  
41                                                          initWithCustomView:blueButton];  
42     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(Cancel)];  
43     self.navigationItem.rightBarButtonItem = button;  
44       
45     [button release];  
46     [blueButton release];  
47 }  
48  

示例图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值