0

我正在创建 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;
}
4

1 回答 1

0

对于高度,您可以执行 self.frame.size.height=30; (或者任何你想要的高度),把它放在 init 方法中。

至于梯度,我不确定

于 2013-03-28T16:53:26.160 回答