IOS--UIButton的使用方法

IOS--UIButton的使用方法详细

(2013-08-23 17:20:38)

转载▼

标签:

ios

uibutton

button

使用详细

it

分类: iOS--UI

// UIButton的常用方法

UIButton *oneButton = [UIButton buttonWithType:UIButtonTypeCustom]; // 初始化时设置Button样式

// 风格有如下

//    typedef enum {

//        UIButtonTypeCustom = 0,           // 自定义,无风格

//        UIButtonTypeRoundedRect,        // 白色圆角矩形,类似偏好设置表格单元或者地址簿卡片

//        UIButtonTypeDetailDisclosure,//蓝色的披露按钮,可放在任何文字旁

//        UIButtonTypeInfoLight,//微件(widget)使用的小圆圈信息按钮,可以放在任何文字旁

//        UIButtonTypeInfoDark,//白色背景下使用的深色圆圈信息按钮

//        UIButtonTypeContactAdd,//蓝色加号(+)按钮,可以放在任何文字旁

//    } UIButtonType;

// 最常用

oneButton.frame = CGRectMake(10, 10, 300, 30); // 设置oneButton的位置和大小

oneButton.backgroundColor = [UIColor blueColor]; // 设置oneButton背景色

oneButton.alpha = 0.8; // 设置oneButton的透明度范围在0.0-1.0之间

[oneButton setTitle:@"我是一个UIButton" forState:UIControlStateNormal]; // 设置在什么状态下显示什么文字

[oneButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];// 设置什么状态下字体什么颜色

[oneButton setBackgroundImage:[UIImage imageNamed:@"image.jpg"]forState:UIControlStateNormal]; // 设置什么状态下显示的背景图像

// 上面几个方法都提到 共同的参数 forState . 这个参数决定了标题、图像或其他属性将在何种状态下显现。你可以编程令按钮在那个状态变化

//    enum {

//        UIControlStateNormal       = 0,//常态

//        UIControlStateHighlighted  = 1 << 0,    //  高亮

//        UIControlStateDisabled     = 1 << 1,    //禁用

//        UIControlStateSelected     = 1 << 2,    // 选中

//        UIControlStateApplication  = 0x00FF0000,// 当应用程序标志使用时

//        UIControlStateReserved     = 0xFF000000 // 为内部框架预留的

//    };

oneButton.tag = 10001; // 设置标签,用于便于点击的是哪个按钮,常用在委托方法中

[oneButton.titleLabel setFont:[UIFont systemFontOfSize:25.0f]]; // 设置文字的大小

oneButton.adjustsImageWhenDisabled = NO; // 对按钮进行微调。当按钮禁用时,图像会被画的颜色深一些 默认是YES,禁止设置为NO

oneButton.adjustsImageWhenHighlighted = NO; // 对按钮进行微调。当按钮高亮是,图像会被画得颜色深一些 默认是YES,禁止设置为NO

oneButton.showsTouchWhenHighlighted = YES; // 设置点击的时候是否有光照得效果

// 给按钮添加响应事件

[oneButton addTarget:self action:@selector(oneButtonTouchUpInside:)forControlEvents:UIControlEventTouchUpInside];//当按钮被按下时会执行oneButtonTouchUpinside:方法。这个方法是自定义的,需要自己写出。参数是(UIButton *)类型

// 添加到view

[self.view addSubview:oneButton];

时间: 2024-08-05 22:53:35

IOS--UIButton的使用方法的相关文章

iOS 开发之 为UIButton添加类别方法加载网络图片

iOS 开发之 为UIButton添加类别方法加载网络图片 使用GCD线程队列实现 工程如下: UIButton+WebCache.h #import <UIKit/UIKit.h> // 为Button添加类别方法 @interface UIButton (WebCache) - (void)xr_setButtonImageWithUrl:(NSString *)urlStr; @end UIButton+WebCache.m #import "UIButton+WebCache

iOS UIButton同时设置title和image属性

在iOS开发中,使用UIButton设置title和image,达到tabBarItem的效果,即title在下,image在上: 目前,我发现有两种比较好的方法: 方法一,使用UIEdgeInsets UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem]; [button setFrame:CGRectMake(100, 100, 60, 60)]; [button setBackgroundColor:[UIColor

iOS开发——实用篇&amp;提高iOS开发效率的方法和工具

提高iOS开发效率的方法和工具 介绍 这篇文章主要是介绍一下我在iOS开发中使用到的一些可以提升开发效率的方法和工具. IDE 首先要说的肯定是IDE了,说到IDE,Xcode不能跑,当然你也可能同时在使用AppCode等其他的IDE,在这里我主要介绍Xcode中提升开发效率的方法. 1.善用快捷键 快捷键是开发中必不可少的,当你善于使用快捷键的时候,十指在键盘上飞舞,那画面太美,我不敢想象. 常用快捷键操作 2.常用代码片段 开发中有一些常用的代码,可以放到代码片段中,然后下次你就可以使用快捷

iOS UIButton按钮

系统字体 + ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]]; NSArray *fontNames; NSInteger indFamily, indFont; for (indFamily=0; indFamily<[familyNames count]; ++indFamily) {     NSLog(@"Fa

iOS开发 UITableView的方法和属性总结

本文描述UITableView的各种方法,属性,委托以及数据源.本文的目的只是总结UITableView的用法,详细的例子另撰文描述. 1 数据源  UITableViewDataSource协议 01 返回组(节)的个数,默认是返回1,如果只有1组数据,可以不用实现该方法. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 02 返回某一组的行数,该组由section的值决定 - (NSInteger)table

IOS UIButton用法详解

这段代码动态的创建了一个UIButton,并且把相关常用的属性都列举了.希望对大家有用.   //这里创建一个圆角矩形的按钮UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; // 能够定义的button类型有以下6种,// typedef enum {// UIButtonTypeCustom = 0, 自定义风格// UIButtonTypeRoundedRect, 圆角矩形 // UIButtonTy

iOS UIButton长按事件

//UIButton *aBtn=[UIButton buttonWithType:UIButtonTypeCustom];//[aBtn setFrame:CGRectMake(40, 100, 60, 60)]; [aBtn setBackgroundImage:[UIImage imageNamed:@"111.png"] forState:UIControlStateNormal];//button点击事件 [aBtn addTarget:self action:@select

ios系统铃声调用方法

首先,这里我要说明这里并不是真正调用系统内部自带的铃声,因为苹果是不允许开发者调用的,没有给开发者接口:如果调用了就无法上线的!那为什么AppStore里面还有那么多app显示的效果是调用系统的铃声一样呢?为什么就能上线呢?其实不然,他们是使用了系统一样的铃声!也就说他们录制和系统铃声相似,然后包含进他们的bundle中来实现的. 所以,我们想要实现调用系统铃声的效果,只能通过这个途径:把音频文件捆绑经我们的bundle中来调用,播放! 给一个网址,里面好多苹果系统铃声(ios8的也有啦....

iOS&#183;UIButton如何文字在下图片在上

创建子类继承自UIButton,在layoutSubviews方法中改变文字和图片的位置就可以了,同理,稍作改变,可以写出文字在上图片在下.本文只给出文字在下图片在上的代码 1 -(void)layoutSubviews { 2 [super layoutSubviews]; 3 4 // image center 5 CGPoint center; 6 center.x = self.frame.size.width/2; 7 center.y = self.imageView.frame.s

iOS动态运行时方法

在某些时候,程序可能需要根据获取的参数来决定调用的方法. 要实现这样的功能,就需要使用到动态运行时方法了. 首先需要定义好接口,以便调用. 然后就是动态调用定义好的方法. 这里有两种方法, 第一种: // 有参数则需要加上冒号 SEL sel = NSSelectorFromString(@"save:"); MyObject obj = [MyObject new]; [obj performSelector:sel withObject:@{@"1":@&quo