创建iOS自定义按钮的任何示例?

时间:2015-03-25 23:26:35

标签: ios uibutton

我想创建一个自定义按钮,它具有自定义背景图像,图标和标签。 它看起来非常类似于我发现的Twitter按钮:

enter image description here

任何好的建议都表示赞赏!

2 个答案:

答案 0 :(得分:0)

是的,您可以在界面构建器中将按钮类型设置为“自定义”。然后,您可以将背景图像,图标,文本和颜色设置为您喜欢的任何颜色。

要获得圆角,您必须添加一些代码:

button.layer.cornerRadius = 10; // Change to the value you desire.
button.clipsToBounds = YES;

答案 1 :(得分:0)

您需要导入石英

#import <QuartzCore/QuartzCore.h>

然后创建按钮就像你的推特按钮一样只需编写这段代码:

UIButton *yourButton = [UIButton buttonWithType:UIButtonTypeCustom];
yourButton.frame = CGRectMake(50, 50, 100, 50);
yourButton.layer.cornerRadius = 10;
yourButton.clipsToBounds = YES;
yourButton.backgroundColor = [UIColor colorWithRed:57.0/255.0 green:178.0/255.0 blue:235.0/255.0 alpha:1.0];

//if you want to set image as background
//[yourButton setBackgroundImage:[UIImage imageNamed:@"background_image"] forState:UIControlStateNormal] 

[yourButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[yourButton setTitle:@"Twitter" forState:UIControlStateNormal];
[yourButton setImage:[UIImage imageNamed:@"twitter_icon"] forState:UIControlStateNormal];