失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 【转载】iphone九宫格界面实现

【转载】iphone九宫格界面实现

时间:2020-01-25 07:14:31

相关推荐

【转载】iphone九宫格界面实现

转自:/iphone-nine-box/

看到CocoaChina很多同学在问,其实很简单,我这是用图片堆砌实现的九宫格,分享如下:

效果图:

核心就这2个方法:

//Power by - (void)viewDidLoad {[super viewDidLoad];NSArray* imageNames = [NSArray arrayWithObjects:@"ico_mobile.png", @"ico_idcard.png", @"ico_postcode.png",@"ico_flight.png", @"ico_translate.png",@"ico_phone.png", @"ico_car.png", @"ico_health.png", @"ico_bjxm.png", nil];UIButton *Btn;for (int i=0; i<9; i++) {CGRect frame;Btn = [[UIButton buttonWithType:UIButtonTypeCustom] retain];[Btn setImage:[UIImage imageNamed:[imageNames objectAtIndex: i]] forState:UIControlStateNormal];//设置按钮图片Btn.tag = i;frame.size.width = 59;//设置按钮坐标及大小frame.size.height = 75;frame.origin.x = (i%3)*(59+32)+40;frame.origin.y = floor(i/3)*(75+24)+40;[Btn setFrame:frame];[Btn setBackgroundColor:[UIColor clearColor]];[Btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:Btn];[Btn release];}}//响应按钮事件-(void)btnPressed:(id)sender{UIButton *Btn = (UIButton *)sender;int index = Btn.tag;switch (index) {case 0:if(mobileController==nil)mobileController = [[MobileController alloc]init];[self.navigationController pushViewController:mobileController animated:YES];break;//其他几个控制器类似}}

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

- (void)loadView {UIImageView *contentView = [[UIImageView alloc] initWithFrame:[[UIScreenmainScreen] applicationFrame]];[contentView setImage:[UIImage imageNamed:@"subview_9_bg.png"]];[contentView setUserInteractionEnabled:YES];self.view = contentView;[contentView release];}

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

@implementation UINavigationBar (CustomImage)- (void)drawRect:(CGRect)rect {UIImage *image = [UIImage imageNamed: @"top_bg.png"];[image drawInRect:CGRectMake(0, 0, self.frame.size.width,self.frame.size.height)];}@end

—-End—-

如果觉得《【转载】iphone九宫格界面实现》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。