首先,寫一個類,繼承於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
摘自 生活不是用來揮霍的