首页 > 移动平台 > 详细

iOS常用技术-九宫格button展示

时间:2016-01-22 02:32:21      阅读:261      评论:0      收藏:0      [点我收藏+]

//
//  CustomButton.m
//  九宫格
//
//  Created by 大欢 on 16/1/21.
//  Copyright © 2016年 bjsxt. All rights reserved.
//

#import "CustomButton.h"

static const CGFloat kScale = 0.7;

@implementation CustomButton

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.imageView.contentMode = UIViewContentModeBottom;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = [UIFont systemFontOfSize:20];
        
    }
    return self;
}

- (CGRect)imageRectForContentRect:(CGRect)contentRect {
    
    CGFloat ponitX = 0;
    CGFloat ponitY = 0;
    CGFloat width = contentRect.size.width;
    CGFloat height = contentRect.size.height * kScale;
    
    return CGRectMake(ponitX, ponitY, width, height);
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect {
    
    CGFloat ponitX = 0;
    CGFloat ponitY = contentRect.size.height * kScale;
    CGFloat width = contentRect.size.width;
    CGFloat height = contentRect.size.height * (1 - kScale);
    
    return CGRectMake(ponitX, ponitY, width, height);
}

@end
/***************************************************/

//
//  ViewController.m
//  九宫格
//
//  Created by 大欢 on 16/1/21.
//  Copyright © 2016年 bjsxt. All rights reserved.
//

#import "ViewController.h"
#import "CustomButton.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    NSArray * nameList = @[@"会议信息",@"会议日程",@"会议讲者",@"会议报道",@"论文摘要",@"会议讨论",@"照片墙",@"参会注册",@"地图",@"报名须知"];
   
    NSArray * imageList = @[@"meeting_info_n",@"meeting_schedule_n",@"meeting_guest_n",@"meeting_new_n",@"meeting_other_n",@"meeting_community_n",@"meeting_image_n",@"meeting_register_n",@"meeting_map_n",@"meeting_notice_n"];
    
    CGFloat width = CGRectGetWidth(self.view.frame)/3;
    CGFloat height = 150;
    
    for (int i = 0; i < nameList.count; i ++) {
        
        CGFloat ponitX = i % 3;
        CGFloat ponitY = i / 3;
        
        CustomButton * btn = [CustomButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(ponitX * width, ponitY * height, width, height);
        [btn setTitle:nameList[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:imageList[i]] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(logTitle:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
    }

}

- (void)logTitle:(UIButton *)button {
    
    NSLog(@"%@",button.currentTitle);
    
}

@end
/************************************************************/

技术分享

iOS常用技术-九宫格button展示

原文:http://www.cnblogs.com/MrWuYindi/p/5149938.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!