如何将背景图像添加到 UIControl
How add background image to UIControl
我创建了第 3 方键盘,并尝试为我的按键添加背景。我使用 UIControl
作为我的键,没有故事板。
我有几种类型的按钮,并尝试为我的角色添加背景UIView
。
let img = UIImage(named:"KeyBackground")
var bgImage: UIImageView?
bgImage = UIImageView(image: img)
bgImage!.frame = keyboardKey.frame
keyboardKey.addSubview(bgImage!)
keyboardKey.sendSubviewToBack(bgImage!)
keyboardKey.backgroundColor = UIColor.clearColor()
但这是行不通的。
我尝试这样添加背景图片:
keyboardKey.backgroundColor = UIColor(patternImage: UIImage(named:"KeyBackground")!)
但是看起来很糟糕...
我做错了什么?
背景看起来像那样,因为你的图像尺寸比每个键的尺寸小一点,并且图像被平铺(重复填充 space - 所以每个键都包含背景右侧、底部和右下角的图像和周围图块的碎片)。
During drawing, the image in the pattern color is tiled as necessary to cover the given area.
您的第一个解决方案可能不起作用,因为您将 UIImageView 作为最底部的子视图插入 - 我假设另一个(不透明的)视图在它上面。您使用的是什么类型的 UIControl?例如,UIButton 已经提供了一些开箱即用的 "background image" 支持。
哦,太简单了。我的 keyboardKey.frame,在调用此方法时,frame = 0, 0, 0, 0.
晚上不适合工作:)
我创建了第 3 方键盘,并尝试为我的按键添加背景。我使用 UIControl
作为我的键,没有故事板。
我有几种类型的按钮,并尝试为我的角色添加背景UIView
。
let img = UIImage(named:"KeyBackground")
var bgImage: UIImageView?
bgImage = UIImageView(image: img)
bgImage!.frame = keyboardKey.frame
keyboardKey.addSubview(bgImage!)
keyboardKey.sendSubviewToBack(bgImage!)
keyboardKey.backgroundColor = UIColor.clearColor()
但这是行不通的。
我尝试这样添加背景图片:
keyboardKey.backgroundColor = UIColor(patternImage: UIImage(named:"KeyBackground")!)
但是看起来很糟糕...
我做错了什么?
背景看起来像那样,因为你的图像尺寸比每个键的尺寸小一点,并且图像被平铺(重复填充 space - 所以每个键都包含背景右侧、底部和右下角的图像和周围图块的碎片)。
During drawing, the image in the pattern color is tiled as necessary to cover the given area.
您的第一个解决方案可能不起作用,因为您将 UIImageView 作为最底部的子视图插入 - 我假设另一个(不透明的)视图在它上面。您使用的是什么类型的 UIControl?例如,UIButton 已经提供了一些开箱即用的 "background image" 支持。
哦,太简单了。我的 keyboardKey.frame,在调用此方法时,frame = 0, 0, 0, 0.
晚上不适合工作:)