我正在创建 UIButton 的子类并在代码中应用样式。出于某种原因,我的按钮没有选择自定义渐变,而且按钮的高度也太小了。如何调整 uibutton 的高度以获得更多填充。
这是代码:
@implementation DefaultButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(id) initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
// Set the button Text Color
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
// Set default backgrond color
[self setBackgroundColor:[UIColor blackColor]];
// Draw a custom gradient
CAGradientLayer *btnGradient = [CAGradientLayer layer];
btnGradient.frame = self.bounds;
btnGradient.colors = [NSArray arrayWithObjects:
(id)[[UIColor colorWithRed:102.0f / 255.0f green:102.0f / 255.0f blue:102.0f / 255.0f alpha:1.0f] CGColor],
(id)[[UIColor colorWithRed:51.0f / 255.0f green:51.0f / 255.0f blue:51.0f / 255.0f alpha:1.0f] CGColor],
nil];
[self.layer insertSublayer:btnGradient atIndex:0];
// Round button corners
CALayer *btnLayer = [self layer];
[btnLayer setMasksToBounds:YES];
[btnLayer setCornerRadius:5.0f];
// Apply a 1 pixel, black border around Buy Button
[btnLayer setBorderWidth:1.0f];
[btnLayer setBorderColor:[[UIColor blackColor] CGColor]];
return self;
}