如何在iOS中创建一个非常自定义的uibutton,如UITableViewCellStyleSubtitle [英] How to create a very custom uibutton like UITableViewCellStyleSubtitle in iOS

查看:114
本文介绍了如何在iOS中创建一个非常自定义的uibutton,如UITableViewCellStyleSubtitle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个按钮,其图像,标题和描述类似于UITableViewCellStyleSubtitle。我想以编程方式执行此操作。

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?

推荐答案

您可以创建UIButton类的类别。以下是您的准系统设置:

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

在.h文件中:

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

在.m文件中:

@implementation UIButton(YourCategoryName)

@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];
}

在上面的代码中,您可以根据需要调整帧数。设置完毕后,只需在view / viewcontroller中创建正常按钮,然后调用上面的方法:

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];

这篇关于如何在iOS中创建一个非常自定义的uibutton,如UITableViewCellStyleSubtitle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆