1 2 3 //MyToolbar.h 头文件 4 @interface MyToolbar : UIToolbar 5 6 @end 7 8 //MyToolbar.m 实现文件 9 #import "MyToolbar.h" 10 11 @implementation MyToolbar 12 13 - (id)initWithFrame:(CGRect)frame 14 { 15 self = [super initWithFrame:frame]; 16 if (self) { 17 18 //设置自定义的MyToolbar为透明背景 19 self.backgroundColor = [UIColor clearColor]; 20 } 21 return self; 22 } 23 24 - (void)drawRect:(CGRect)rect 25 { 26 // Drawing code 27 } 28 29 @end 30 31 32 33 //HomeViewController.h 头文件(调用mytoolbar的页面) 34 #import "MyToolbar.h" 35 36 @interface HomeViewController () 37 38 @end 39 40 //HomeViewController.m 实现文件 41 42 @implementation HomeViewController 43 44 45 - (void)viewDidLoad 46 { 47 [super viewDidLoad]; 48 49 //创建MyToolbar 50 MyToolbar *toolbar = [[MyToolbar alloc]initWithFrame:CGRectMake(0, 22, 320, 44)]; 51 52 //添加到当前视图中 53 [self.view addSubview:toolbar]; 54 } 55 56 57 58