Atma
Atma

Reputation: 29775

How to create a very custom uibutton like UITableViewCellStyleSubtitle in iOS

I would like to create a button with an image, title and description similar to the UITableViewCellStyleSubtitle. I would like to do this programmatically.

Does anyone know how I can accomplish this?

Upvotes: 2

Views: 4749

Answers (2)

Chamara Paul
Chamara Paul

Reputation: 720

You could create a category of the UIButton class. Here's a barebones setup for you:

In the .h file:

@interface UIButton (YourCategoryName)
    - (void)setupButton:(NSString *)title image:(UIImage *)image description:(NSString *) description;
@end

In the .m file:

@implementation UIButton (YourCategoryName)

- void)setupButton:(NSString *)title image:(UIImage *)image description:(NSString *) description {
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 200, 50)]];
    [self addSubview:titleLabel];
    [titleLabel release];

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:image];
    [self addSubview:imageView]; 
    [imageView release];

    UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 60, 200, 100)];
    [self addSubview:descriptionLabel];
    [descriptionLabel release];
}

In the above code, you can adjust the frames as needed. Once this has been setup, just create your button like normal in your view/viewcontroller and call the method above:

  UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
  myButton.frame = CGRectMake(0, 0, 300, 200);
  [myButton setupButton:myTitle image:myImage description:myDesc];

Upvotes: 6

titaniumdecoy
titaniumdecoy

Reputation: 19251

Subclass UIControl and override drawRect and/or add subviews to create the desired appearance.

Upvotes: 2

Related Questions