iOS 自定义button

来源:互联网 发布:一开守望先锋网络就卡 编辑:程序博客网 时间:2024/05/16 15:10

这次写的并不算是一种功能,它只是方便我们去快速的铺界面,先来看下效果图

创建这种button,多数人会用image+label等组合模式来创建,这种创建的方法不仅费时,还比较费力,今天我带来的是自定义button,仅需要两行代码,就能实现这种效果


首先,创建个自定义button类

////  BackGroundButton.h//  Life////  Created by Amydom on 16/7/5.//  Copyright © 2016年 Amydom. All rights reserved.//#import <UIKit/UIKit.h>@interface BackGroundButton : UIButton@end

////  BackGroundButton.m//  Life////  Created by Amydom on 16/7/5.//  Copyright © 2016年 Amydom. All rights reserved.//#import "BackGroundButton.h"@implementation BackGroundButton//title的位置- (CGRect)titleRectForContentRect:(CGRect)contentRect{    return CGRectMake(0, 53, 375/3, 20);}//背景的位置- (CGRect)backgroundRectForBounds:(CGRect)bounds{    return CGRectMake((375/3 - 31) / 2, 13, 31, 31);}@end

下面在viewcontroller里使用就可以了

////  ViewController.m//  Life////  Created by Amydom on 16/7/5.//  Copyright © 2016年 Amydom. All rights reserved.//#import "ViewController.h"#import "BackGroundButton.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    self.view.backgroundColor = [UIColor colorWithWhite:0.925 alpha:1.000];    [self createMySubViews];}- (void)createMySubViews{    //banner    UIImageView *bannerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, 375, 200)];    bannerImageView.image = [UIImage imageNamed:@"banner.png"];    [self.view addSubview:bannerImageView];        //放button的背景view    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, bannerImageView.frame.size.height + bannerImageView.frame.origin.y + 20, 375, 200)];    backView.backgroundColor = [UIColor whiteColor];    [self.view addSubview:backView];    //存放列表图片和标题    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"myAssetsButtonImageAndTitle" ofType:@"plist"];    NSArray *imageAndtiltleArray = [[NSArray alloc] initWithContentsOfFile:plistPath];    for (int i = 0; i < [imageAndtiltleArray count]; i++) {        //自定义button         BackGroundButton *btn = [BackGroundButton buttonWithType:UIButtonTypeCustom];        btn.frame = CGRectMake(375 / 3 * (i % 3), backView.frame.size.height / 2 * (i / 3), 375 / 3, backView.frame.size.height / 2);        [btn setBackgroundImage:[UIImage imageNamed:[imageAndtiltleArray objectAtIndex:i][@"image"]] forState:UIControlStateNormal];        [btn setTitle:[imageAndtiltleArray objectAtIndex:i][@"title"] forState:UIControlStateNormal];        [btn setTitleColor:[UIColor colorWithRed:58 / 255.0 green:58 / 255.0 blue:58 / 255.0 alpha:1.0] forState:UIControlStateNormal];        btn.titleLabel.textAlignment = NSTextAlignmentCenter;        btn.titleLabel.font = [UIFont systemFontOfSize:12.0f];        btn.tag = i;        [btn addTarget:self action:@selector(setButtonBackGroundColor:) forControlEvents:UIControlEventTouchUpInside];                //设置边界的宽度        [btn.layer setBorderWidth:0.3];                //设置按钮的边界颜色        [btn.layer setBorderColor:[UIColor colorWithWhite:0.925 alpha:1.000].CGColor];                [backView addSubview:btn];    }            }#pragma mark - ClickEvent- (void)setButtonBackGroundColor:(id)sender {    UIButton *AssetsBtn = (UIButton *) sender;    switch (AssetsBtn.tag) {        case 0: {          //话费充值界面                    } break;        case 1: {            //流量充值界面                   } break;        case 2: {            //预约挂号界面                          } break;        case 3: {            //客运购票界面                          } break;        case 4: {            //违章查询界面                } break;        case 5: {            //更多界面                    } break;         default:            break;    }}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

这里为了方便,我把数据存放到pllist文件里了,这样,这种图文的button就创建完成了,感觉还是很简单的(这个自定义button类可以直接放到工程中使用..)




1 0
原创粉丝点击