首先,写一个类,继承于UIButton。
头文件:
Ios代码 #import@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
示例图:
![](http://dl.iteye.com/upload/attachment/597014/cb8bec00-82db-308b-bd6a-153aa5d8ad14.jpg)