我已经使用了几个小时,并且正在努力寻找解决方案。我创建了一个UIButton
并添加了渐变背层。我要做的是在按钮上添加UIImage
。我尝试使用setImage(image:, forState:)
方法添加按钮。该图像显示在调试器的视图层次结构中,但不会出现在模拟器或iPad上。我还尝试添加自定义UIView
并将其作为子视图添加到UIButton
。我得到了相同的结果。有关如何在设备或模拟器上显示图像的任何想法?
deleteKey.frame = CGRect(x: 719, y: 10, width: 134.0, height: 63.0)
deleteKey.backgroundColor = UIColor.clearColor()
altGradients.append(CAGradientLayer())
altGradients[altGradients.count - 1].frame = CGRect(x: 0.0, y: 0.0, width: 134.0, height: 63.0)
altGradients[altGradients.count - 1].startPoint = CGPoint(x: 0.5, y: 0.0)
altGradients[altGradients.count - 1].endPoint = CGPoint(x: 0.5, y: 1.0)
altGradients[altGradients.count - 1].colors = altOffColor
altGradients[altGradients.count - 1].cornerRadius = 5.0
deleteKey.layer.addSublayer(altGradients[altGradients.count - 1])
deleteKey.adjustsImageWhenHighlighted = false
deleteKey.setImage(UIImage(named: "keyboard_delete.png")?.imageWithRenderingMode(.AlwaysOriginal), forState: .Normal)
deleteKey.addTarget(self, action: Selector("altKeyPressed:"), forControlEvents: .TouchDown)
deleteKey.addTarget(self, action: Selector("altKeyReleased:"), forControlEvents: .TouchUpInside)
altKeys.append(deleteKey)
self.addSubview(deleteKey)
答案 0 :(得分:0)
为什么不在xcode中添加图像视图并使用所需的按钮图像,然后在其前面添加UI按钮并删除文本?这不需要任何编程,而且耗时少且复杂得多。
答案 1 :(得分:0)
// Delete key
deleteKey.frame = CGRect(x: 719, y: 10, width: 134.0, height: 63.0)
altGradients.append(CAGradientLayer())
altGradients[altGradients.count - 1].frame = CGRect(x: 0.0, y: 0.0, width: 134.0, height: 63.0)
altGradients[altGradients.count - 1].startPoint = CGPoint(x: 0.5, y: 0.0)
altGradients[altGradients.count - 1].endPoint = CGPoint(x: 0.5, y: 1.0)
altGradients[altGradients.count - 1].colors = altOffColor
altGradients[altGradients.count - 1].cornerRadius = 5.0
deleteKey.layer.addSublayer(altGradients[altGradients.count - 1])
deleteImage.image = UIImage(named: "keyboard_delete.png")
deleteImage.sizeToFit()
deleteImage.center = CGPoint(x: deleteKey.frame.width / 2, y: deleteKey.frame.height / 2)
deleteKey.addSubview(deleteImage)
deleteKey.backgroundColor = UIColor.clearColor()
deleteKey.addTarget(self, action: Selector("altKeyPressed:"), forControlEvents: .TouchDown)
deleteKey.addTarget(self, action: Selector("altKeyReleased:"), forControlEvents: .TouchUpInside)
altKeys.append(deleteKey)
self.addSubview(deleteKey)