我有一个自定义组件,按照 this 完成教程。该组件是可重用的,我需要他的背景是透明的,但不是 subview (我有三个 UIImageView、一个 UIButton 和一个标签。
我试过这个:
self.view.backgroundColor = UIColor.purpleColor().colorWithAlphaComponent(0)
这样做,如果我将 alpha 设置为高于 0,背景将变为紫色。但是,如果我将 alpha 设置为 0,背景将变为白色,而不是透明的( View 具有背景图像)。
有人可以帮助我吗?谢谢。
编辑:
忘了说了,view上面有一个UIScrollView。
编辑 2:
在 Main.storyboard 的 ViewController 中:
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "fondo")!)
根据要求进一步说明:
我按照我之前链接的教程在他自己的 xib 文件中创建了一个自定义组件。
为了使用它,我将一个 View 对象拖到 main.storyboard,并将他的类设置为对应于该 xib 的 swift。
因此,我有一个用于 Main.storyboard 的 ViewController,以及一个用于符合我的自定义组件的 xib 的 swift 文件。也就是说,两个 swift 文件,一个用于 Main.storyboard,一个用于自定义 component.xib。
我在 Main.storyboard (ViewController.swift) 的 swift 文件中设置了背景图像。如果我做任何其他事情,我有我的自定义组件,但背景是白色的。所以,我想出了“我需要为自定义组件 (custoncomponcnt.swift) 设置背景透明”
代码: 自定义 View .swift
import UIKit
@IBDesignable class CustomView: UIView {
// Our custom view from the XIB file
var view: UIView!
@IBOutlet weak var label: UILabel!
@IBOutlet weak var icono: UIImageView!
@IBAction func onClick(sender: UIButton) {
println("button")
}
@IBInspectable var image: UIImage? {
get {
return icono.image
}
set(image) {
icono.image = image
}
}
@IBInspectable var text: String? {
get {
return label.text
}
set (text){
label.text=text
}
}
func xibSetup() {
view = loadViewFromNib()
// use bounds not frame or it'll be offset
view.frame = bounds
// Make the view stretch with containing view
view.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
//self.view.backgroundColor = UIColor.purpleColor().colorWithAlphaComponent(0)
self.view.backgroundColor=UIColor.clearColor()
// Adding custom subview on top of our view (over any custom drawing > see note below)
addSubview(view)
}
func loadViewFromNib() -> UIView {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: "CustomView", bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
return view
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
}
*/
override init(frame: CGRect) {
// 1. setup any properties here
// 2. call super.init(frame:)
super.init(frame: frame)
// 3. Setup view from .xib file
xibSetup()
}
required init(coder aDecoder: NSCoder) {
// 1. setup any properties here
// 2. call super.init(coder:)
super.init(coder: aDecoder)
// 3. Setup view from .xib file
xibSetup()
}
}
ViewController.swift:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "fondo")!)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
最佳答案
设置颜色为 UIColor.clearColor()
关于ios - 自定义组件背景透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32779329/