button 笔记
来源:互联网 发布:苹果电脑删除不了软件 编辑:程序博客网 时间:2024/06/08 04:38
button的使用 :
首先创建:
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
其中UIButtonTypeRoundedRect表示什么样式的button 除了这个圆角按钮的话还有
UIButtonTypeContactAdd
UIButtonTypeDetailDisclosure
UIButtonTypeInfoDark
UIButtonTypeInfoLight
UIButtonTypeCustom
这些都是UIButtonType枚举中的
UIButtonTypeCustom比较特殊他是自定义按钮下面说
//案例
//创建圆角按钮
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
//设置按钮上的标题
//UIControlStateNormal 正常状态下的样式
[btn setTitle:@"圆角"forState:UIControlStateNormal];
[btn setTitle:@"圆角点击" forState:UIControlStateHighlighted];
//设置按钮上的标题颜色
[btn setTitleColor:[UIColororangeColor]forState:UIControlStateNormal];
//点击事件
[btn addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside]; //UIControlEventTouchUpInside表示点击触发
//设置标题上de字体大小
btn.titleLabel.font = [UIFontsystemFontOfSize:30];
//按钮位置
btn.frame = CGRectMake(0,0,300, 30);
//设置 按钮 识别标记
btn.tag = 1;
//将按钮加入视图中
[self.view addSubview:btn];
下面是自定义button -UIButtonTypeCustom
自定义button 可以重写
-(CGRect)titleRectForContentRect:(CGRect)contentRect
{
}-(CGRect)imageRectForContentRect:(CGRect)contentRect
{
}
//两个方法
案例
MyButton.m
#import "MyButton.h"
@implementation MyButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
-(CGRect)titleRectForContentRect:(CGRect)contentRect
{
return CGRectMake(0,0,150, 30);
}
-(CGRect)imageRectForContentRect:(CGRect)contentRect
{
return CGRectMake(150,0,150, 30);
}
@end
#import "MainViewController.h"
//使用自定义button的使用#import "MyButton.h"
// 自定义按钮(只有自定义按钮可以修改图片和文字的位置)
MyButton *btn3 = [MyButtonbuttonWithType:UIButtonTypeCustom];
btn3.frame = CGRectMake(10,150,300, 100);
btn3.backgroundColor = [UIColorredColor];
// 设置按钮背景图片(小图片被拉伸,大图片被压缩不会超出frame大小)
[btn3 setBackgroundImage:[UIImageimageNamed:@"ico_7.png"]forState:UIControlStateNormal];
// 设置按钮上层的图片(小图不会被拉伸且居中显示,大图片被压缩不会超出frame大小)
// [btn3 setImage:[UIImage imageNamed:@"ico_7.png"] forState:UIControlStateNormal];
[btn3 setTitle:@"自定义按钮" forState:UIControlStateNormal];
[btn3 addTarget:selfaction:@selector(btnClick:)forControlEvents:UIControlEventTouchUpInside];
btn3.tag = 3;
[self.view addSubview:btn3];
//上面的小demo地址http://download.csdn.net/detail/u010486174/6321325
添加
当按下时发光 属性:
button.highlighted = NO;
button.reversesTitleShadowWhenHighlighted = NO;
button.showsTouchWhenHighlighted = NO;
- button 笔记
- wpf学习笔记---Button
- Android 笔记 之 Button
- MFC Radio Button笔记
- MFC Radio Button笔记
- Android开发笔记:button
- MFC Radio Button笔记
- ionic button 实用笔记
- Android学习笔记:Button
- Android笔记:自定义Button
- Qt笔记-qml-button
- 【Android笔记】(3)Button控件
- android笔记3-button,imageButton
- zephyr学习笔记---CC3200---Button
- zephyr学习笔记---CC3200---Button
- UGUI 学习笔记 6 Button
- 【代码笔记】iOS-点击一个button,出6个button
- android学习笔记之button的建立
- 2
- 中标麒麟,安装jdk1.7,提示`GLIBC_2.4' not found
- eclipse中在项目中查找指定文件
- 关于设计模式2
- GWT 中的Cookies的使用
- button 笔记
- c:import 标签详解
- javascript面向对象(给对象添加属性和方法的方式)
- DWORD变量在内存中和纯单引号的串在内存中存储顺序
- 新浪微博SSO授权以及分享(实战)
- OpenCV 2 学习笔记(22): 腐蚀与膨胀
- 今晚的恒大比赛
- Linux命令(2):ls命令
- 黑马程序员------Java的注解与类加载器