当前位置:编程学习 > wap >>

自定义导航栏按钮

首先,写一个类,继承于UIButton。
 
头文件:
Ios代码 
#import <Foundation/Foundation.h> 
 
@inte易做图ce 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 


摘自 生活不是用来挥霍的

 


补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,