iOS UI基础-16.0 UIButton

时间:2023-03-08 22:05:08
iOS UI基础-16.0 UIButton

回归自然,UIButton是我们使用最频烦的一个控件。下面,对该控件的一些常用方法进行一些总结.

    UIButton *payStateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
payStateBtn.frame = CGRectMake(, , ScreenWidth - , ); // 设置字体向左对齐
payStateBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
// 设置字体
payStateBtn.titleLabel.font = [UIFont systemFontOfSize:];
[payStateBtn setTitle:@"支付失败" forState:UIControlStateNormal];
[payStateBtn setTitle:@"支付成功" forState:UIControlStateSelected];
[payStateBtn setTitleEdgeInsets:UIEdgeInsetsMake(, , , )];
[payStateBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[payStateBtn setTitleColor:[UIColor colorWithHexString:Color_Red] forState:UIControlStateSelected];
[payStateBtn setImage:[UIImage imageNamed:@"failure"] forState:UIControlStateNormal];
[payStateBtn setImage:[UIImage imageNamed:@"success"] forState:UIControlStateSelected];
[self.View addSubview:payStateBtn];

上面的代码,很常用,我们在看代码,已经大概知道其中意思。

要显示选中时的样子,只需要设置:payStateBtn.Selected = Yes;

另外一些常用的属性

设置不可点击

[Button setUserInteractionEnabled:NO];
// 或者

 button.userInteractionEnabled = NO;

设置中心位置点

btn1.center = CGPointMake(, );

设置按钮样式

//    能够定义的button类型有以下6种,
// typedef enum {
// UIButtonTypeCustom = 0, 自定义风格
// UIButtonTypeRoundedRect, 圆角矩形
// UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用
// UIButtonTypeInfoLight, 亮色感叹号
// UIButtonTypeInfoDark, 暗色感叹号
// UIButtonTypeContactAdd, 十字加号按钮
// } UIButtonTyp

其它

   /* forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现*/
//以下是几种状态
// enum {
// UIControlStateNormal = 0, 常规状态显现
// UIControlStateHighlighted = 1 << 0, 高亮状态显现
// UIControlStateDisabled = 1 << 1, 禁用的状态才会显现
// UIControlStateSelected = 1 << 2, 选中状态
// UIControlStateApplication = 0x00FF0000, 当应用程序标志时
// UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管他
// };
/*
* 默认情况下,当按钮高亮的情况下,图像的颜色会被画深一点,如果这下面的这个属性设置为no,
* 那么可以去掉这个功能
*/
button1.adjustsImageWhenHighlighted = NO;
/*跟上面的情况一样,默认情况下,当按钮禁用的时候,图像会被画得深一点,设置NO可以取消设置*/
button1.adjustsImageWhenDisabled = NO;
/* 下面的这个属性设置为yes的状态下,按钮按下会发光*/
button1.showsTouchWhenHighlighted = YES; /* 给button添加事件,事件有很多种,我会单独开一篇博文介绍它们,下面这个时间的意思是
按下按钮,并且手指离开屏幕的时候触发这个事件,跟web中的click事件一样。
触发了这个事件以后,执行butClick:这个方法,addTarget:self 的意思是说,这个方法在本类中
也可以传入其他类的指针*/
[button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];

设置圆角和边框及边框颜色

[box.actionButton.layer setMasksToBounds:YES];
[box.actionButton.layer setCornerRadius:10.0]; //设置矩形四个圆角半径
//边框宽度
[box.actionButton.layer setBorderWidth:1.0];
//设置边框颜色有两种方法:第一种如下:
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ , , , });
[box.actionButton.layer setBorderColor:colorref];//边框颜色
//第二种方法如下:
//_testButton.layer.borderColor=[UIColor grayColor].CGColor;

设置按钮图片在上面,文字在下面

重写UIButton

@implementation XMGVerticalButton

- (void)setup
{
self.titleLabel.textAlignment = NSTextAlignmentCenter;
} - (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self setup];
}
return self;
} - (void)awakeFromNib
{
[self setup];
} - (void)layoutSubviews
{
[super layoutSubviews]; // 调整图片
self.imageView.x = ;
self.imageView.y = ;
self.imageView.width = self.width;
self.imageView.height = self.imageView.width; // 调整文字
self.titleLabel.x = ;
self.titleLabel.y = self.imageView.height;
self.titleLabel.width = self.width;
self.titleLabel.height = self.height - self.titleLabel.y;
} @end

如果想让按钮点击后,立即就显示,不需要过度效果,需要重写UIButton

#import "HWEmotionTabBarButton.h"

@implementation HWEmotionTabBarButton

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// 设置文字颜色
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self setTitleColor:[UIColor darkGrayColor] forState:UIControlStateDisabled];
// 设置字体
self.titleLabel.font = [UIFont systemFontOfSize:];
}
return self;
} - (void)setHighlighted:(BOOL)highlighted {
// 按钮高亮所做的一切操作都不在了
}
@end