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
- ios button 自定义
- iOS-自定义button
- iOS学习(自定义button)
- iOS总结(自定义button)
- ios自定义button
- iOS 自定义button
- iOS中自定义button、button属性、swift
- iOS自定义形状的Button
- ios 自定义Button 不能点击问题
- iOS—自定义UISearchBar Cancel Button
- IOS 自定义button有效点击范围
- IOS自定义图片放右边的Button
- iOS 自定义UITableViewCell中button事件处理
- iOS自定义按钮button详解封装说明
- 仿IOS开关button 自定义ios开关控件
- 自定义button
- 自定义BUTTON!
- 自定义button
- Homebrew指令集
- Spring - bean配置-FactoryBean
- 判断当前ViewPager中Fragment是否可见
- HDU - 1166 A - 敌兵布阵 线段树单点更新模板
- VS2015中Qt5.7和OpenCV2.4.10配合打开图片和视频
- iOS 自定义button
- php/mysql 工作总结
- CentOS 7 修改网卡名称为 eth0
- Flink的运行(官方文档翻译)
- 利用python进行数据分析——pandas
- 去除代码中的注释(一)设计思想
- POJ - 3468 B - A Simple Problem with Integers 线段树区间更新模板
- 常用的String类方法
- 第3.1.6章 WEB系统最佳实践 js控件之bootstrap table