0

I'd like to be able to customize any buttons I'll create within a specific style of my app. I was trying to create a new class which will consist of all my settings, so every time I add a new button I can use methods of the class to customize the new button.

I'm a very new to the iOS programming so I quess I might've been doing something wrong, but that's what I've got so far:

I created the class with settings

#import "CustomButton.h"

@implementation CustomButton

-(UIButton *) setButtonWithType{

    self.layer.cornerRadius = 25;
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    self.layer.shadowOffset = CGSizeMake(1.5f, 1.5f);
    self.layer.shadowOpacity = 1.0f;
    self.layer.shadowRadius = 0.0f;
    self.layer.masksToBounds = NO;

    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = self.bounds;
    gradient.startPoint = CGPointMake(0.5, 0.5);
    gradient.endPoint = CGPointMake(0.0, 0.5);
    gradient.colors = [NSArray arrayWithObjects:(id)[UIColor whiteColor].CGColor,(id)[UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0].CGColor, nil];
    gradient.cornerRadius = self.layer.cornerRadius;
    [self.layer insertSublayer:gradient atIndex:0];

    return self;
}

@end

ViewController.h

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIButton *button;

@end

ViewController.m

#import "ViewController.h"
#import "CustomButton.h"

@interface ViewController ()


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    CustomButton *btn = [[CustomButton alloc] init];


    self.button = [btn setButtonWithType]; 

//rest of the code

Any ideas how to make it works? Or do I need to use a very different approach?

Thank you!!

Syngmaster
  • 415
  • 8
  • 13
  • you don't use any nibs/storyboard right? your newly alloc'd init'd button has no frame or anything and it doesn't belong to a superview :/ – Daij-Djan Feb 14 '17 at 17:02
  • No, I do use the storyboard, so the button loads with its general setting and when I try to apply new ones, it doesn't work... – Syngmaster Feb 14 '17 at 17:10
  • 1
    you should declare the iboutlet with CUstomButton class intead of UIBUtton! You dont have to assign customButton to the self.button property. – Teja Nandamuri Feb 14 '17 at 17:14
  • Do what Teja Nandamuri says. And pack your custom initialization in one method that is called from `init`, `initWithFrame` and `initWithCoder:`. You can find a hint [here](http://stackoverflow.com/a/15279646/653513). The way you are handling it is overriding the logic of xib loading/initializing. – Rok Jarc Feb 14 '17 at 17:18
  • Customizing buttons is not always the best idea. Consider customizing `UIControl` instead. – Sulthan Feb 14 '17 at 17:32

1 Answers1

1

You may find success doing this as a Category (lots of examples out there)...

UIButton+ShadowRoundedGradient.h

#import <UIKit/UIKit.h>

@interface UIButton (ShadowRoundedGradient)

- (void) makeMe;

@end

UIButton+ShadowRoundedGradient.m

#import "UIButton+ShadowRoundedGradient.h"

@implementation UIButton (ShadowRoundedGradient)

- (void) makeMe {

    self.layer.cornerRadius = 25;
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    self.layer.shadowOffset = CGSizeMake(1.5f, 1.5f);
    self.layer.shadowOpacity = 1.0f;
    self.layer.shadowRadius = 0.0f;
    self.layer.masksToBounds = NO;

    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = self.bounds;
    gradient.startPoint = CGPointMake(0.5, 0.5);
    gradient.endPoint = CGPointMake(0.0, 0.5);
    gradient.colors = [NSArray arrayWithObjects:(id)[UIColor whiteColor].CGColor,(id)[UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0].CGColor, nil];
    gradient.cornerRadius = self.layer.cornerRadius;
    [self.layer insertSublayer:gradient atIndex:0];

}

@end

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {

@property (weak, nonatomic) IBOutlet UIButton *button;

}

@end

ViewController.m

#import "ViewController.h"
#import "UIButton+ShadowRoundedGradient"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.button makeMe];

    //rest of the code
}
DonMag
  • 69,424
  • 5
  • 50
  • 86
  • Thanks! That's exactly what I need! I think I got confused myself between a class and a category. Need to get a recap on the Obj-C theory :) – Syngmaster Feb 14 '17 at 17:48