UIButton
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UIButton相关的知识,希望对你有一定的参考价值。
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)dealloc
{
self.window = nil;
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
self.window.rootViewController = [[UIViewController alloc] init];
// UIControl 添加了状态和事件
//UIButton -> UIControl -> UIView -> NSObject
/* buttontype
UIButtonTypeRoundedRect == UIButtonTypeSystem
UIButtonTypeDetailDisclosure !号的按钮
UIButtonTypeContactAdd + 号的按钮
UIButtonTypeCustom 自定义图片时使用 !!!
*/
//使用类方法创建button
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(50, 50, 100, 50);
/* state
UIControlStateNormal 正常
UIControlStateDisabled 禁用
UIControlStateHighlighted 高亮
UIControlStateSelected 选择
*/
//设置标题, 根据不同状态
[button setTitle:@"normal" forState:UIControlStateNormal];
[button setTitle:@"disabled" forState:UIControlStateDisabled];
[button setTitle:@"highLighted" forState:UIControlStateHighlighted];
[button setTitle:@"selected" forState:UIControlStateSelected];
//设置选择状态 缺省 NO
// button.selected = YES;
//使能开关 缺省 YES
// button.enabled = NO;
//设置全局的颜色 主题颜色
button.tintColor = [UIColor blackColor];
//根据状态来设置颜色
[button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
/* Events 事件 !!!
UIControlEventTouchUpInside
UIControlEventTouchUpOutside
UIControlEventTouchDragInside
UIControlEventTouchDragOutside
*/
//异步编程模型
//添加无参的button事件
// [button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
//添加带参数的button事件
//传递的参数就是添加事件的对象
//(点击并在按钮范围内松手)
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor cyanColor];
[self.window addSubview:button];
//system不支持自定义的图片
//使用自定义的图片的button,要使用custom样式!!!
UIButton *imageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
imageBtn.backgroundColor = [UIColor blackColor];
imageBtn.frame = CGRectMake(50, 150, 100, 100);
//获取图片
// 1.png可以不写拓展名 jpg必须写
// 2.不支持gif
// 3. UIImageView UIImage 表示图片类
UIImage *image = [UIImage imageNamed:@"image"];
//设置button的图片
[imageBtn setImage:image forState:UIControlStateNormal];
[imageBtn setImage:[UIImage imageNamed:@"image2"] forState:UIControlStateSelected];
[imageBtn setTitle:@"xxxxxxxx" forState:UIControlStateNormal];
[imageBtn addTarget:self action:@selector(imageChange:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:imageBtn];
UIButton *imgBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
imgBtn1.backgroundColor = [UIColor blackColor];
imgBtn1.frame = CGRectMake(50, 300, 100, 100);
//设置背景图片
[imgBtn1 setBackgroundImage:image forState:UIControlStateNormal];
[imgBtn1 setTitle:@"xxxxxxx" forState:UIControlStateNormal];
[self.window addSubview:imgBtn1];
/*
backgroundImage image的区别
1.background 伸缩图片, image只有小于图片时伸缩
2.background 文字浮于图片上方
image 文字和图片水平并列
*/
return YES;
}
- (void)imageChange:(UIButton *)button
{
// if (button.isSelected)
// {
// button.selected = NO;
// }
// else
// {
// button.selected = YES;
// }
//
button.selected = !button.isSelected;
#if 0
static BOOL flag = NO;
NSString *imageName = nil;
if (flag)
{
imageName = @"image";
// flag = NO;
}
else
{
imageName = @"image2";
// flag = YES;
}
UIImage *image = [UIImage imageNamed:imageName];
[button setImage:image forState:UIControlStateNormal];
flag = !flag;
#endif
}
- (void)click:(UIButton *)button
{
//获取button的标题, titleLabel只读
NSLog(@"%@", button.titleLabel.text);
//删除一个事件, 跟添加的内容相同
[button removeTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)click
{
NSLog(@"click");
}
以上是关于UIButton的主要内容,如果未能解决你的问题,请参考以下文章