iPhone的九宫格实现代码

核心就这2个方法:

  1. //Power by ieliwb.com  
  2. - (void)viewDidLoad {  
  3.     [super viewDidLoad];  
  4.       
  5.     NSArray* imageNames = [NSArray arrayWithObjects:  
  6.                                         @"ico_mobile.png",   
  7.                                         @"ico_idcard.png",   
  8.                                         @"ico_postcode.png",  
  9.                                         @"ico_flight.png",   
  10.                                         @"ico_translate.png",  
  11.                                         @"ico_phone.png",   
  12.                                         @"ico_car.png",   
  13.                                         @"ico_health.png",   
  14.                                         @"ico_bjxm.png", nil];  
  15.    
  16.     UIButton *Btn;  
  17.     for (int i=0; i<9; i++) {  
  18.         CGRect frame;  
  19.         Btn = [[UIButton buttonWithType:UIButtonTypeCustom] retain];  
  20.         [Btn setImage:[UIImage imageNamed:[imageNames objectAtIndex: i]] forState:UIControlStateNormal];//设置按钮图片  
  21.           
  22.         Btn.tag = i;  
  23.           
  24.         frame.size.width = 59;//设置按钮坐标及大小  
  25.         frame.size.height = 75;  
  26.         frame.origin.x = (i%3)*(59+32)+40;  
  27.         frame.origin.y = floor(i/3)*(75+24)+40;  
  28.         [Btn setFrame:frame];  
  29.           
  30.         [Btn setBackgroundColor:[UIColor clearColor]];  
  31.         [Btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];  
  32.         [self.view addSubview:Btn];  
  33.         [Btn release];  
  34.           
  35.     }  
  36.           
  37. }  
  38.    
  39. //响应按钮事件  
  40. -(void)btnPressed:(id)sender{  
  41.     UIButton *Btn = (UIButton *)sender;  
  42.         int index = Btn.tag;  
  43.       
  44.     switch (index) {  
  45.         case 0:  
  46.             if(mobileController==nil)  
  47.                 mobileController = [[MobileController alloc]init];  
  48.             [self.navigationController pushViewController:mobileController animated:YES];  
  49.             break;  
  50.         //其他几个控制器类似  
  51.     }  
  52.    
  53. }  

九宫格背景修改可以这样实现:

[cpp:nogutter] view plaincopy
  1. - (void)loadView {  
  2.     UIImageView *contentView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];  
  3.     [contentView setImage:[UIImage imageNamed:@"subview_9_bg.png"]];  
  4.     [contentView setUserInteractionEnabled:YES];  
  5.     self.view = contentView;  
  6.     [contentView release];  
  7. }  

UINavigationBar背景图片可以这样实现:

[cpp:nogutter] view plaincopy
  1. @implementation UINavigationBar (CustomImage)  
  2. - (void)drawRect:(CGRect)rect {  
  3.     UIImage *image = [UIImage imageNamed: @"top_bg.png"];  
  4.     [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];  
  5. }  
  6. @end 
转自:http://www.ieliwb.com/iphone-nine-box/
原文地址:https://www.cnblogs.com/hopeanCom/p/2781880.html