iOS开发API UIButton


UIButton是iOS开发中经常使用的组件,下边把UIButton的API记录一下:

//创建圆角button

     UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];      

  //指定button的位置和大小

     button.frame = CGRectMake(10, 10, 75, 75);

  //给button设置标签,用来辨别点击的是哪个button,常用在委托方法中。

     button.tag = 0;

  //定义按钮标题

     [button setTitle:@"Button Title" forState:UIControlStateNormal];

  //定义按钮标题字体格式

     [button.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];

  //给button添加委托方法,即点击触发的事件。

     [button addTarget:selfaction:@selector(touchButton1:)  forControlEvents :UIControl EventTouchUp Inside];

  //给button添加图片 

     [button setImage:[UIImageimageNamed:@"blue_bg(2).png"] forState:UIControlStateNormal];

  //将button加入视图当中

     [view addSubview:button];

项目经常用到的.m文件可查找以下代码段:

//
//  ViewController.m
//  UIButtonDemo
//
//  Created by Yen Joe on 12-2-29.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"

@implementation ViewController

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle
-(void)btnPressed:(id)sender{
    UIButton* btn = (UIButton*)sender;
    //开始写你自己的动作
    [[[[UIAlertView alloc]initWithTitle:@"Button" message:[NSString stringWithFormat: @"This is button:%@ action" ,btn]delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]autorelease]show];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    //两种不同的方式创建
    UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 80, 44)];
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	
    //设置Frame
    CGRect btn2Frame = CGRectMake(100.0, 100.0, 60.0, 44.0);
    btn2.frame =btn2Frame;
    
    //设置Title
    [btn1 setTitle:@"BTN1" forState:UIControlStateNormal];
    [btn2 setTitle:@"BTN2" forState:UIControlStateNormal];
    [btn2 setBackgroundImage:[UIImage imageNamed:@"pic.png"] forState:UIControlStateNormal];
    
    [btn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];//设置标题颜色
    [btn1 setTitleShadowColor:[UIColor grayColor] forState:UIControlStateNormal ];//阴影
    [btn1 setBackgroundImage:[UIImage imageNamed:@"PIC"] forState:UIControlStateHighlighted];//背景图像
    btn1.adjustsImageWhenHighlighted = NO;
    btn1.adjustsImageWhenDisabled = NO;
    btn1.showsTouchWhenHighlighted = NO;
    
    [self.view addSubview:btn1];
    [self.view addSubview:btn2];
    
    [btn1 addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];//添加动作
    
  /*  - (CGRect)imageRectForContentRect:(CGRect)bounds{
        return CGRectMake(0.0, 0.0, 44, 44);
    }*///重绘行为
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值