⑴ iOS的UI开发中Button的基本编写方法解析
一、简单说明
 
 一般情况下,点击某个控件后,会做出相应反应的都是按钮
 
 按钮的功能比较多,既能显示文字,又能显示图片,还能随时调整内部图片和文字的位置
 
 二、按钮的三种状态
 
 normal(普通状态)
 
 默认情况(Default)
 
 对应的枚举常量:UIControlStateNormal
 
 highlighted(高亮状态)
 
 按钮被按下去的时候(手指还未松开)
 
 对应的枚举常量:UIControlStateHighlighted
 
 disabled(失效状态,不可用状态)
 
 如果enabled属性为NO,就是处于disable状态,代表按钮不可以被点击
 
 对应的枚举常量:UIControlStateDisabled
 
 三、注意点
 
 (1)从Xcode5开始,图片资源都放到Images.xcassets中进行管理,可以使用拖拽的方式添加项目中用到的图片到Images.xcassets中
 
 (2)若干多个控件共用一段代码,通常使用tag。
 
 四、代码示例
 
 (1)
 
 复制代码 代码如下:
 
 #import "LFViewController.h"
 
 @interface LFViewController ()
 
 @property (weak, nonatomic) IBOutlet UIButton *headImageView;
 
 @end
 
 @implementation LFViewController
 
 // 在OC中,绝大多数的控件的监听方法的第一个参数就是控件本身
 
 //- (IBAction)left:(UIButton *)button {
 
 //
 
 // NSLog(@"----");
 
 //}
 
 - (IBAction)move
 
 {
 
 // 通过frame修改head的位置
 
 // 在OC中,不允许直接修改“对象”的“结构体属性”的“成员”
 
 // 允键塌许修改“对象”的'“结构体属性”
 
 // 1. 取出结构体属性
 
 CGRect rect = self.headImageView.frame;
 
 // 2. 修改结构体成员
 
 rect.origin.y -= 20;
 
 // 3. 设置对象的结构体属性
 
 self.headImageView.frame = rect;
 
 }
 
 稿谈圆(2)
 
 复制代码 代码如下:
 
 #import "LFViewController.h"
 
 使用git
 
 1. 创建项目时,勾选git
 
 2. 开发告一段落后,选择"Source Control""Commit",并编写注释
 
 // 枚举类型实侍早质上就是一个整数,作用就是用来替代魔法数字
 
 // 枚举类型中,指定了第一个整数之后,后面的数字会递增
 
 typedef enum
 
 kMovingDirTop = 10,
 
 kMovingDirBottom,
 
 kMovingDirLeft,
 
 kMovingDirRight,
 
 } kMovingDir;
 
 #define kMovingDelta 50
 
 @interface LFViewController ()
 
 @property (weak, nonatomic) IBOutlet UIButton *headImageView;
 
 @end
 
 @implementation LFViewController
 
 - (IBAction)move:(UIButton *)button
 
 // CGRect rect = self.headImageView.frame;
 
 CGPoint p = self.headImageView.center;
 
 // magic number魔法数字,其他程序员看到代码的时候,不知道是什么意思
 
 switch (button.tag) {
 
 case kMovingDirTop:
 
 p.y -= kMovingDelta;
 
 break;
 
 case kMovingDirBottom:
 
 p.y += kMovingDelta;
 
 break;
 
 case kMovingDirLeft:
 
 p.x -= kMovingDelta;
 
 break;
 
 case kMovingDirRight:
 
 p.x += kMovingDelta;
 
 break;
 
 [UIView beginAnimations:nil context:nil];
 
 [UIView setAnimationDuration:1.0];
 
 self.headImageView.center = p;
 
 [UIView commitAnimations];
 
 - (IBAction)zoom:(UIButton *)button
 
 CGRect rect = self.headImageView.bounds;
 
 // 在C语言中,关于bool的判断:非零即真
 
 if (button.tag) {
 
 rect.size.width += 50;
 
 rect.size.height += 50;
 
 rect.size.width -= 50;
 
 rect.size.height -= 50;
 
 // 首尾动画
 
 // beginAnimations表示此后的代码要“参与到”动画中
 
 [UIView beginAnimations:nil context:nil];
 
 [UIView setAnimationDuration:2.0];
 
 self.headImageView.bounds = rect;
 
 // self.headImageView.alpha = 0;
 
 // commitAnimations,将beginAnimation之后的所有动画提交并生成动画
 
 [UIView commitAnimations];
 
 @end
 
 五、补充笔记
 
 1. IBAction的参数
 
 - (IBAction)left:(UIButton *)button
 
 (1) 在OC中,绝大多数的控件监听方法的第一个参数就是控件本身
 
 (2) 默认连线时的参数类型是id
 
 (3) 如果要在监听方法中,方便控件的使用,可以在连线时或者连线后,修改监听方法的参数类型
 
 2. 修改对象的结构体成员
 
 在OC中,不允许直接修改“对象”的“结构体属性”的“成员”,但是允许修改“对象”的“结构体属性”
 
 修改结构体属性的成员方法如下:
 
 (1)使用临时变量记录对象的结构体属性
 
 (2) 修改临时变量的属性
 
 (3)将临时变量重新设置给对象的结构体属性
 
 3. 在程序开发中需要避免出现魔法数字(Magic Number)
 
 使用枚举类型,可以避免在程序中出现魔法数字
 
 (1)枚举类型实质上就是一个整数,其作用就是用来替代魔法数字
 
 (2)枚举类型中,指定了第一个整数之后,后面的数字会递增
 
 4. frame & bounds & center
 
 1> frame可以修改对象的位置和尺寸
 
 2> bounds可以修改对象的尺寸
 
 3> center可以修改对象的位置
 
 5. 首尾式动画
 
 复制代码 代码如下:
 
 // beginAnimations表示此后的代码要“参与到”动画中
 
 [UIView beginAnimations:nil context:nil];
 
 // setAnimationDuration用来指定动画持续时间
 
 [UIView setAnimationDuration:2.0];
 
 self.headImageView.bounds = rect;
 
 ......
 
 // commitAnimations,将beginAnimation之后的所有动画提交并生成动画
 
 [UIView commitAnimations];
 
 下面来罗列一下UIButton的基本属性罗列
 
 第一、UIButton的定义
 
 复制代码 代码如下:
 
 UIButton *button=[[UIButton buttonWithType:(UIButtonType);
 
 能够定义的button类型有以下6种,
 
 复制代码 代码如下:
 
 typedef enum {
 
 UIButtonTypeCustom = 0, 自定义风格
 
 UIButtonTypeRoundedRect, 圆角矩形
 
 UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用
 
 UIButtonTypeInfoLight, 亮色感叹号
 
 UIButtonTypeInfoDark, 暗色感叹号
 
 UIButtonTypeContactAdd, 十字加号按钮
 
 }UIButtonType;
 
 第二、设置frame
 
 复制代码 代码如下:
 
 button1.frame = CGRectMake(20, 20, 280, 40);
 
 [button setFrame:CGRectMake(20,20,50,50)];
 
 第三、button背景色
 
 复制代码 代码如下:
 
 button1.backgroundColor = [UIColor clearColor];
 
 [button setBackgroundColor:[UIColor blueColor]];
 
 第四、state状态
 
 forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现
 
 复制代码 代码如下:
 
 enum {
 
 UIControlStateNormal = 0, 常规状态显现
 
 UIControlStateHighlighted = 1 << 0, 高亮状态显现
 
 UIControlStateDisabled = 1 << 1, 禁用的状态才会显现
 
 UIControlStateSelected = 1 << 2, 选中状态
 
 UIControlStateApplication = 0x00FF0000, 当应用程序标志时
 
 UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管他
 
 @property(nonatomic,getter=isEnabled)BOOL enabled; // default is YES. if NO, ignores touch events and subclasses may draw differently
 
 @property(nonatomic,getter=isSelected)BOOL selected; // default is NO may be used by some subclasses or by application
 
 @property(nonatomic,getter=isHighlighted)BOOL highlighted;
 
 第五 、设置button填充图片和背景图片
 
 复制代码 代码如下:
 
 [buttonsetImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];
 
 [buttonsetBackgroundImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];
 
 第六、设置button标题和标题颜色
 
 复制代码 代码如下:
 
 [button1 setTitle:@"点击" forState:UIControlStateNormal];
 
 [buttonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];
 
 第七、设置按钮按下会发光
 
 复制代码 代码如下:
 
 button.showsTouchWhenHighlighted=NO;
 
 第八、添加或删除事件处理
 
 复制代码 代码如下:
 
 [button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
 
 [btn removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
 
 第九、 设置按钮内部图片间距和标题间距
 
 复制代码 代码如下:
 
 UIEdgeInsets insets; // 设置按钮内部图片间距
 
 insets.top = insets.bottom = insets.right = insets.left = 10;
 
 bt.contentEdgeInsets = insets;
 
 bt.titleEdgeInsets = insets; // 标题间距
⑵ iOS 调整UIButton 图片(imageView)与文字(titleLabel)的位置
 UIButton可以同时设置Title和Image,UIButton有两个属性:titleEdgeInsets(top,left,bottom,right)和imageEdgeInsets(top,left,bottom,right),通过设置这两个,就可以实现所有需要的Button的样式
   UIButton 的 默认状态下imageEdgeInsets = UIEdgeInsetsMake(0,0,0,0);titleEdgeInsets = UIEdgeInsetsMake(0,0,0,0);  图片在左文字在右,而且整体水平和垂直居中  。比如下面这个图文按钮:
为了最美观一点,可以设置图标与文字间距 。如下图:
设置图片在右文字在左:
设置图片在上,文字在下:
设置图片左对齐:
设置文字右对齐:
设置文字左对齐,图片右对齐:
⑶ ios中怎么设置uibutton上字体的大小
1.在IOS程序中设置UIButton的字体大小:
btn.frame = CGRectMake(x, y, width, height);
[btn setTitle: @"search" forState: UIControlStateNormal];
//设置按钮上的自体的大小
//[btn setFont: [UIFont systemFontSize: 14.0]];    //这种可以用来设置字体的大小,但是可能会在将来的SDK版本中去除改方法
//应该使用
btn.titleLabel.font = [UIFont systemFontOfSize: 14.0];
[btn seBackgroundColor: [UIColor blueColor]];
//最后将按钮加入到指定视图superView
[superView addSubview: btn];
附:创建按钮的两种方法:
1、动态创建
btnfont = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnfont setFrame:CGRectMake(100, 10, 120, 40)];
[btnfont addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
[btnfont setTitle:@"字体" forState:UIControlStateNormal];
btnfont.backgroundColor=[UIColor clearColor];
[self.view addSubview:btnfont];
2、在xib文件中已经创建好,通过tag获取按钮
UIButton *testButton= (UIButton*)[self.view viewWithTag:100];
[testButton addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];
注册事件
-(void) test: (id) sender{
UIAlertView *av = [[[UIAlertView alloc] initWithTitle:@"ceshi" message:@"test11111" delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil] autorelease];
[av show];
}
2.设置UIButton的文字显示位置、字体的大小、字体的颜色
btn.frame = CGRectMake(x, y, width, height);
[btn setTitle: @"search" forState: UIControlStateNormal];
//设置按钮上的自体的大小
//[btn setFont: [UIFont systemFontSize: 14.0]];    //这种可以用来设置字体的大小,但是可能会在将来的SDK版本中去除改方法
//应该使用
btn.titleLabel.font = [UIFont systemFontOfSize: 14.0];
[btn seBackgroundColor: [UIColor blueColor]];
//最后将按钮加入到指定视图superView
[superView addSubview: btn];
tvnamelabel=[[UIButton alloc]initWithFrame:CGRectMake(5,5,200,40)];
这样初始化的button,文字默认颜色是白色的,所有如果背景也是白色的话,是看不到文字的,
btn.contentHorizontalAlignment= ;//设置文字位置,现设为居左,默认的是居中
[btn setTitle:@“title”forState:UIControlStateNormal];// 添加文字
有些时候我们想让UIButton的title居左对齐,我们设置
btn.textLabel.textAlignment = UITextAlignmentLeft
是没有作用的,我们需要设置
btn.contentHorizontalAlignment = ;
但是问题又出来,此时文字会紧贴到做边框,我们可以设置
btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);
使文字距离做边框保持10个像素的距离。
设置UIButton上字体的颜色设置UIButton上字体的颜色,不是用:
[btn.titleLabel setTextColor:[UIColorblackColor]];
btn.titleLabel.textColor=[UIColor redColor];
而是用:
[btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
⑷ ios 怎么让button中的文字紧靠下边框
如果是xib上拖拽的,点击button,右侧第四个检查器,type选择custom;如果是代码创族咐建的UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom]; 如果button是文正携字, [button setTitle:@"哈哈举穗伏" forState:UIControlStateNormal]
⑸ iOS button 按钮 设置渐变或者layer之后无法展示文字或者图片
CAGradientLayer *gl = [CAGradientLayer layer];
  
  gl.frame = CGRectMake(0,0,100,24);
  
 gl.startPoint = CGPointMake(0, 0);
  
  毁坦粗 gl.endPoint = CGPointMake(1, 1);
  
   gl.colors = @[( __bridge   id )[UIColor colorWithRed:0/255.0 green:168/255.0 blue:79/255.0 alpha:1.0].CGColor,( __bridge   id )[UIColor colorWithRed:26/255.0 green:186/255.0 blue:74/255.0 alpha:1.0].CGColor];
  
 gl.locations = @[@(0.0),@(1.0f)];
  
  [_continueStudyBtn.layer addSublayer:gl];
  
 1.解决方法信搏。背景放在最底层纤镇
  
    [_continueStudyBtn.layer insertSublayer:gl atIndex:0];
  
     [_continueStudyBtn bringSubviewToFront:_continueStudyBtn.imageView];
⑹ ios当button加载完成已后,怎么改变button的文字
如果你用的是IB,选中按钮在右侧属性界面进行设置:按钮类型(Type)设置为自定义,BackgroundImage选择你的图片,Title内输入你要显示的文字 代码实现的话如下: //按钮初始化 UIButton *myButton =[UIButton buttonWithType:UIButtonTypeCustom]; (注意此处使用便利构造器初始化的button,不需release) //设置按钮图片 [myButton setBackgroundImage:[UIImage imageNamed:@"myPic.png"] forState:UIControlStateNormal]; //设置文字 [myButton setTitle:@"确定" forState:UIControlStateNormal];
⑺ iOS开发 UIButton的基本属性
Button就是一个按钮,就是手机界面中可以点击的那种(点击之后跳转入另一个界面) 
  
 Button定义时有许多类型可以选择
一般情况下使用custom属于自定义型 
  
  1、定义一个Button按钮  (UIButton *按钮名;)     并且将其定义为自定义类型 
  
  2、button的大小  (按钮名+.frame) 
  
  3、button显示的字体名称加状态 
  
        (1)  一般使用这三种状态  ([按钮名 setTitle: forState:]) 
  
  参数一:设置按钮标题(String类型) 
  
  参数二:设置按钮显示标题时所处的状态(系统自带) 
  
       (2)  这种定义标题的方式  不常用  (按钮名+.titleLabel.text) 
  
  4、button的背景颜色  (按钮名+.backgroundColor) 
  
   5、设置Button的背景图片 
  
  ([按钮名 setBackgroundImage:  forState:]) 
  
  参数一:添加的图片 参数二:按钮所处状态 
  
  注意 
  
     背景图像会根据按钮的尺寸拉伸
  
     按钮图像会居中显示在按钮中央位置
  
     如果同时设置了按钮的图像和文字
  
     按钮区域足够大,会并列显示图像和文字
  
     如果区域不够大,优先显示图像
  
 6、 设置Button的title颜色 
  
  ([按钮名 setTitleColor: forState:]) 
  
  参数一:选择颜色 参数二:按钮所处的类型 
  
 7、 给Button添加点击事件 
  
  ([按钮名 addTarget: action:  forControlEvents:]) 
  
  参数  一:  对象  (self为自身)        
  
  参数  二:  @selector(点击事件的方法名称) 
  
  参数  三:  为事件出发的类型 例如touch触碰,upinside点击 
  
  下面是在ViewDidLoad外面重新定义的一个方法用于点击了按钮 
  
  点击事件函数(当按钮点击时,运行函数) 
  
  一般页面之间的跳转,也在点击函数中添加 
  
  8、显示在当先视图下 
                                                         首先是为normal下  
                                                                     高亮状态下 
以上就是按钮的一些最普通的属性
  
 谢谢观看!若是有错误,可以私信!谢谢
⑻ iOS开发 设置button的image的位置
开巧搜发过程中经常遇到通过imageEdgeInsets和titleEdgeInsets去孝野历设置button中的image,title的位置,来达到想要的效果。但因为对其原理的不了解,经常碰壁,设置了却达不到自己想要的效果。终于找到这篇文章,文章作者解析的很详细,这里记录一下,方便自己日后脊或查阅。
   1.常用的button样式,图片在上,文字在下
⑼ ios开发怎么让button上的image紧跟着字
button有一个属圆逗性,用来设置图片和文春橘字的偏移量的
[button setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0)];
[button setTitleEdgeInsets:UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0)];
根据你的需要自己调试到合适的位置即可。橘森卖
⑽ IOS UIButton 文字不全,被压缩了。比如"P..y"
如果你没有使用AutoLayout
你需要改变button.frame = CGRectMake(xx, xx, 能够显示全文字的合适宽度, 比字体大2.0f的高度)
如果你使用了AutoLayout
你需要改变button的width Constraint至能够显示全文字的合适宽度.