iPhone的九宮格實現代碼

來源:互聯網
上載者:User

核心就這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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.