UIButton的使用

使用UIButton时需要注意的是:

1.UIButton的创建有专门的类方法(buttonWithType:,UILabel没有);

2.UIButton常用的属性包括:frame、titile、tag等;

3.UIButton使用addTarget方法关联处理方法,一个UIButton可以有多个不同的按钮响应方法,多个UIButton也可以共享一个处理方法(用tag做区分);

// 创建文件按钮
- (void) createRectUI {
    // 穿件圆角矩形按钮
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn1.frame = CGRectMake(100, 100, 100, 50);
    [btn1 setTitle:@"按钮1" forState:UIControlStateNormal];
    btn1.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.view addSubview:btn1];

    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn2.frame = CGRectMake(100, 200, 100, 50);
    [btn2 setTitle:@"按钮2" forState:UIControlStateNormal];
    btn2.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.view addSubview:btn2];

    btn1.tag = 101;
    [btn1 addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
    btn2.tag = 102;
    [btn2 addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
}

- (void) btnPressed : (UIButton*) btn {
    NSInteger tag = btn.tag;
    if(tag == 101){
        NSLog(@"btn1 pressed!");
    }else{
        NSLog(@"btn2 pressed!");
    }
}

  

时间: 2024-08-06 19:33:06

UIButton的使用的相关文章

自定义UIButton

#import <UIKit/UIKit.h> #import "UIView+SDExtension.h" @interface CookButton : UIButton @end #import "CookButton.h" @implementation CookButton - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame])

UIButton样式设置

btn.frame = CGRectMake(x, y, width, height); [btn setTitle: @”search” forState: UIControlStateNormal]; //设置按钮上的自体的大小 //[btn setFont: [UIFont systemFontSize: 14.0]];    //这种可以用来设置字体的大小,但是可能会在将来的SDK版本中去除改方法 //应该使用 btn.titleLabel.font = [UIFont systemFo

UIKit框架之UIButton详解

UIKit框架是iPhone应用程序开发中最基本的框架,也是用得最多.最重要的框架,今天要和大家分享的就是UIKit中的UIButton相关知识,一起来看看吧. 1.实例化: 1.1.init方式: 1 UIButton *button = [[UIButton alloc] initWithFrame:rect]; 1.2.类方法方式: 1 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 其中按钮类型枚

IOS 自定义UIBUTTON 直接拖个xib 就能在button上显示多行文本 并且添加了点击的效果

拖个button继承一下  几行代码 就搞定 自用效果还行 IOS 自定义UIBUTTON 直接拖个xib 就能在button上显示多行文本 并且添加了点击的效果,布布扣,bubuko.com

iOS 中UIButton的 settitle 和 titlelabel的使用误区

UIButton中设置Titl方法包括以下几种: - (void)setTitle:(NSString *)title forState:(UIControlState)state; - (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)state @property(nonatomic,readonly,retain) NSString *currentTitle; @property(n

iOS 强大的泛型,同样也可以对UIButton进行扩展

文章围绕这五点: 1. 泛型是什么 2. 为什么要用泛型 3. 泛型怎么用 4. 泛型进阶 5. 泛型的延伸使用 泛型(Generics)是什么? 引用Apple中Generics的描述: Generic code enables you to write flexible, reusable functions and types that can work with any type, subject to requirements that you define. You can writ

UIButton的imageEdgeInsets 和 titleEdgeInsets

我们知道,在UIButton中有一个UILabel和一个UIImageView,同时还有属性: titleEdgeInsets,imageEdgeInsets.介绍下 imageEdgeInsets 和 titleEdgeInsets 的用法. UIEdgeInsets 首先,titleEdgeInsets 和 imageEdgeInsets 都是 UIEdgeInsets类型.UIEdgeInsets 是一个结构体,定义如下: typedef struct UIEdgeInsets { CGF

iOS UIButton EdgeInsets

说一下系统的button,image 和 title的位置关系 默认image 和 title的位置关系: 随便画了草图,有点丑,不过不妨碍理解: 第一种:在button上只设置文字,这个时候,button的文字默认是剧中的. 第二种:在button上只设置图片,也是默认剧中的. 第三种:主要说的是这种,当同时设置图片和文字时,默认图片是剧中的,文字就会被排挤到button的右侧. 这个时候,如果我们想要改变文字和button的位置,就要使用EdgeInsets EdgeInsets UIEdg

4. UIButton的使用

1. UIButton的初认识 来自:http://www.cnblogs.com/mcj-coding/p/5103891.html QQ:853740091 1.1 UIButton 是iOS 开发中常用的控件之一,主要用于用户触摸屏幕触发相应的事件,比如你点击了app一个界面就会跳转到另一个界面,这个功能很有可能就是使用了UIButton. UIButton 继承自UIControl类,UIControl类主要的作用是将复杂的触摸事件封装成了简单的易于使用的控件事件.例如通过UIContr

自定义UIButton及注意点

一.自定义UIButton /* 调整Button内部子控件的步骤 1.自定义Button 2.调整位置 1>重写两个方法:titleRectForContentRect:和imageRectForContentRect: 2>重写layoutSubviews: 先调用super方法.之后自己调整frame 3.如果需要设置imageView和titleLabel的属性时,在initWithFrame:方法设置 */ // 1.创建UIButton对象 XMGButton *btn = [XM