给UIButton添加(类别)方法,方便批量创建button
来源:互联网 发布:便携电脑推荐 知乎 编辑:程序博客网 时间:2024/06/05 15:44
转载自: http://code4app.com/snippets/one/%E7%BB%99UIButton%E6%B7%BB%E5%8A%A0-%E7%B1%BB%E5%88%AB-%E6%96%B9%E6%B3%95-%E6%96%B9%E4%BE%BF%E6%89%B9%E9%87%8F%E5%88%9B%E5%BB%BAbutton/51f0e2d36803fab305000006#s0
.h
//
// UIButton+Create.h
//
// Created by 魏建科 on 13-7-25.
// Copyright (c) 2013年 魏建科. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface
UIButton
(Create)
+ (UIButton*)
createButtonWithFrame
: (CGRect) frame
Target
:(
id
)target
Selector
:(
SEL
)selector
Image
:(
NSString
*)image
ImagePressed
:(
NSString
*)imagePressed;
+ (
UIButton
*)
createButtonWithFrame
:(CGRect)frame
Title
:(
NSString
*)title
Target
:(
id
)target
Selector
:(
SEL
)selector;
@end
.m
//
// UIButton+Create.m
//
// Created by 魏建科 on 13-7-25.
// Copyright (c) 2013年 魏建科. All rights reserved.
//
#import "UIButton+Create.h"
@implementation
UIButton
(Create)
+ (UIButton*)
createButtonWithFrame
: (CGRect) frame
Target
:(
id
)target
Selector
:(
SEL
)selector
Image
:(
NSString
*)image
ImagePressed
:(
NSString
*)imagePressed
{
UIButton
* button = [
UIButton
buttonWithType
:UIButtonTypeCustom];
[button
setFrame
:frame];
UIImage
*newImage = [
UIImage
imageNamed
:
image
];
[button
setBackgroundImage
:newImage
forState
:UIControlStateNormal];
UIImage
*newPressedImage = [
UIImage
imageNamed
:
imagePressed
];
[button
setBackgroundImage
:newPressedImage
forState
:UIControlStateHighlighted];
[button
addTarget
:target
action
:selector
forControlEvents
:UIControlEventTouchUpInside];
return
button;
}
+ (
UIButton
*)
createButtonWithFrame
:(CGRect)frame
Title
:(
NSString
*)title
Target
:(
id
)target
Selector
:(
SEL
)selector
{
UIButton
* button = [
UIButton
buttonWithType
:UIButtonTypeRoundedRect];
[button
setFrame
:frame];
[button
setTitle
:title
forState
:UIControlStateNormal];
[button
addTarget
:target
action
:selector
forControlEvents
:UIControlEventTouchUpInside];
return
button;
}
@end
使用方法,首先在要使用的ViewController
包含#import "UIButton+Create
.h
"
然后使用方法
- (
void
)viewDidLoad
{
[
super
viewDidLoad
];
[
self
.view
addSubview
:[
UIButton
createButtonWithFrame
:CGRectMake(
0
,
0
,
1
6
0
,
4
0
)
Title
:
@"测试"
Target
:
self
Selector
:
@selector
(buttonPressed:)]];
[
self
.view
addSubview
:[
UIButton
createButtonWithFrame
:CGRectMake(
0
,
1
0
0
,
1
6
0
,
4
0
)
Target
:
self
Selector
:
@selector
(buttonPressed:)
Image
:
@"这个是你给这个Button设置的默认图片"
ImagePressed
:
@"button点击后显示的高亮的图片"
]];
}
- (
void
)buttonPressed:(
id
)sender
{
}
0 1
- 给UIButton添加(类别)方法,方便批量创建button
- iOS给UIButton添加类别,批量添加Button
- swift给UIbutton添加闭包扩展,方便,简单实用
- 给多个button批量添加监听器
- 给button添加弧度的方法
- iOS 【UIKit-给Button添加监听方法】
- iOS 开发之 为UIButton添加类别方法加载网络图片
- iOS 开发之 为UIButton添加类别方法加载网络图片
- iOS 给类别添加属性
- 别人的批量创建UIButton
- 给Button添加边框
- 给 UIButton、UILabel添加下划线
- 给Button控件添加js脚本验证的方法
- 对比:UIButton创建方法
- 批量给pdf添加目录(最完整详细方法)
- iOS 给类别里面添加属性
- 利用runtime给类别添加属性
- iOS 如何给类别添加属性
- springmvc+spring+mybatis整合心得
- ids for this class must be manually assigned before calling save():
- HEVC中SAO--自适应样点补偿 详细分析解读
- 应用与MPlayer通过slave模式进行信息交换
- OC 的 #pragma mark - 在Swift 里的代替方法
- 给UIButton添加(类别)方法,方便批量创建button
- UIImage图片缩放到指定大小尺寸
- 探究javascript mvc(二)
- HEVC细琐杂项 持续更新~
- 多路归并排序
- 虚拟机复制后网络启动失败的解决办法
- Tokyo Cabinet和Tokyo Tyrant安装和测试
- 关于编写面向对象的时钟问题
- UVa12356 - Army Buddies(水题)