1.设置button的tag值
int k;
button.tag = k;
2.Button禁止触摸事件的2种方式
//会改变按钮的状态,颜色会变灰
button.enabled = NO;
//保持按钮原来的状态,颜色不会变
button.userInteractionEnabled = NO;
3.全部圆角/部分圆角
1.关键字:UIBezierPath
2.关键API:
+ (UIBezierPath *)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii
3.关键词组:
enum {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0};
typedef NSUInteger UIRectCorner;
用法示例:
//初始话View
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(120, 10, 80, 80)];
view2.backgroundColor = [UIColor redColor];[self.view addSubview:view2];
//对View的左下角和右下角实现圆角效果
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view2.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = view2.bounds;
maskLayer.path = maskPath.CGPath;view2.layer.mask = maskLayer;
//实现四个角都有圆角效果
view2.layer.masksToBounds = YES;
view2.layer.cornerRadius = 5;
Button文本左对齐
无效写法:
// button.titleLabel.textAlignment = NSTextAlignmentLeft;
NSTextAlignmentLeft针对UILabe有效
有效写法:
//对齐方式修改为水平左对齐,但是会紧紧靠着左边
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//按钮的内容(控件)距离左边20个像素,这样就可以了
button.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);