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

any example for creating an iOS custom button?

我想创建一个自定义按钮,它有自定义背景图片、图标和标签。 它与我发现的 Twitter 按钮非常相似:

如有好的建议,我们将不胜感激!

是的,您可以在界面生成器中将按钮类型设置为 "custom"。然后您可以将背景图像、图标、文本和颜色设置为您喜欢的任何颜色。

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

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

您需要导入石英

#import <QuartzCore/QuartzCore.h>

然后要创建类似您的 Twitter 按钮的按钮,只需编写以下代码:

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