如何将按钮图标添加到自定义键盘iOS 8?

z9zf31ra  于 5个月前  发布在  iOS
关注(0)|答案(3)|浏览(57)

我正在为iOS 8创建自定义键盘。像这样创建键盘按钮

UIButton *aBtn = [ UIButton buttonWithType:UIButtonTypeCustom ];

    [aBtn setFrame:CGRectMake(x, 30, btnWidth, btnHeight)];

    [aBtn setImage:[UIImage imageNamed:@"A"] forState:UIControlStateNormal];

    [aBtn setTitle:@"A" forState:UIControlStateNormal];
    [aBtn setTitleColor:[ UIColor blackColor] forState:UIControlStateNormal];
    [aBtn setTitleColor:[ UIColor whiteColor] forState:UIControlStateHighlighted];
    [aBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:aBtn];

字符串
我的问题是图像没有设置为按钮。我如何解决这个问题?
我是否需要任何特殊步骤才能为自定义键盘添加图像到Xcode?

ekqde3dh

ekqde3dh1#

您可能没有将图像添加到键盘目标。
检查您的键盘目标>构建阶段>复制捆绑包资源,以确保图像在那里。如果没有,将其拖到那里。
请注意,我说的是键盘目标,而不是主机应用程序。这可能是这里的混乱。

myss37ts

myss37ts2#

您可以按照以下步骤在自定义键盘上添加图像。

1.在xcode项目中添加图片

2.在键盘包资源中添加图片,请查看下面的图片


的数据

3.设置图像背景按钮。检查下面的图像


unguejic

unguejic3#

在您的自定义键盘文件夹中添加新的“资产目录”(“KeyboardViewController.swift”文件所在的位置)。


的数据
然后将您的图像添加到“资产目录”文件夹中,并使用以下代码。

let button = UIButton(type: UIButton.ButtonType.system) as UIButton     
button.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
if let image  = UIImage(named: "image_name") { // no need to use ".png format"
    button.setBackgroundImage(image, for: .normal)
}
button.backgroundColor = .red // to show button position
view.addSubview(button)

字符串

相关问题