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;