Wednesday, 3 April 2013

Gradient UIButton

To make a Gradient UIButton follow below code snippet:


    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setFrame:CGRectMake(90, 120, 140, 32)];
    [btn setTitle:@"Gradient Button" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
    [self.view addSubview:btn];
    
    CAGradientLayer *layer1 = [CAGradientLayer layer];
    NSArray *colors = [NSArray arrayWithObjects:
                       (id)[UIColor lightGrayColor].CGColor,
                       (id)[UIColor blackColor].CGColor,
                       nil];
    [layer1 setColors:colors];
    [layer1 setFrame:btn.bounds];
    [btn.layer insertSublayer:layer1 atIndex:0];
    btn.clipsToBounds = YES;


Final output will look as follows:


No comments:

Post a Comment