如何在Xib中设置Button的属性(圆角以及背景颜色)

大家可能比较习惯用代码设置Button的属性,圆角、背景图片、背景颜色什么的都比较容易设置,可是如果在xib中创建的Button,又该如何来改变呢,今天我就遇到了这个问题。将解决的办法分享给大家:

1.设置圆角

直接在xib文件所对应的类的.m文件中重写- (void)drawRect:(CGRect)rect方法:

- (void)drawRect:(CGRect)rect{

[super drawRect:rect];
self.playerBtn.layer.cornerRadius = 3.0;
self.playerBtn.layer.masksToBounds = YES;

}


2.设置背景颜色(不是背景图片)

[self.playerBtn setBackgroundImage:[UIImage imageWithColor:RGBA(0, 127, 180, 1.0)] forState:UIControlStateNormal];
[self.playerBtn setBackgroundImage:[UIImage imageWithColor:RGBA(171, 77, 197, 1.0)] forState:UIControlStateHighlighted];

+ (UIImage*) imageWithColor: (UIColor*) color

{

    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);

    UIGraphicsBeginImageContext(rect.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetFillColorWithColor(context, [color CGColor]);

    CGContextFillRect(context, rect);

    UIImage*theImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return theImage;

}


这里的+ (UIImage*) imageWithColor: (UIColor*) color方法是我定义在UIImage+ImageEffects类目中的一个方法


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值