博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iPhone的九宫格实现代码
阅读量:4030 次
发布时间:2019-05-24

本文共 1926 字,大约阅读时间需要 6 分钟。

很多开发者会在 iPhone 软件里用到手机上经典的九宫格界面,本段实现九宫格的代码由 CocoaChina 会员花太香齐 分享,博客原文地址 http://www.ieliwb.com/iphone-nine-box/ 核心就这2个方法: //Pow

很多开发者会在 iPhone 软件里用到手机上经典的九宫格界面,本段实现九宫格的代码由 CocoaChina 会员“花太香齐” 分享,博客原文地址 

核心就这2个方法:

//Power by ieliwb.com

- (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:[[UIScreen mainScreen] 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

转载地址:http://xkrbi.baihongyu.com/

你可能感兴趣的文章
通过C++反射实现C++与任意脚本(lua、js等)的交互(二)
查看>>
利用清华镜像站解决pip超时问题
查看>>
[leetcode BY python]1两数之和
查看>>
微信小程序开发全线记录
查看>>
Centos import torchvision 出现 No module named ‘_lzma‘
查看>>
Maximum Subsequence Sum
查看>>
PTA:一元多项式的加乘运算
查看>>
CCF 分蛋糕
查看>>
解决python2.7中UnicodeEncodeError
查看>>
小谈python 输出
查看>>
Django objects.all()、objects.get()与objects.filter()之间的区别介绍
查看>>
python:如何将excel文件转化成CSV格式
查看>>
Django 的Error: [Errno 10013]错误
查看>>
机器学习实战之决策树(一)
查看>>
[LeetCode By Python] 2 Add Two Number
查看>>
机器学习实战之决策树二
查看>>
[LeetCode By Python]7 Reverse Integer
查看>>
[LeetCode By Python]9. Palindrome Number
查看>>
[leetCode By Python] 14. Longest Common Prefix
查看>>
[LeetCode By Python]108. Convert Sorted Array to Binary Search Tree
查看>>