【发布时间】:2015-09-25 09:43:10
【问题描述】:
我有一个自定义组件,按照this 教程完成。该组件是可重用的,我需要他的背景是透明的,但不是子视图(我有三个 UIImageView,一个 UIButton 和一个标签。
我试过这个:
self.view.backgroundColor = UIColor.purpleColor().colorWithAlphaComponent(0)
这样做,如果我将 alpha 设置为高于 0,则背景变为紫色。但是,如果我将 alpha 设置为 0,则背景变为白色,而不是透明(视图有背景图像)。
有人可以帮助我吗?谢谢。
编辑:
忘了说,视图上方有一个 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) 设置背景透明”
代码: Customview.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.
}
}
【问题讨论】:
-
请添加背景图片的代码。
-
设置图案后不要再设置背景色。请参阅我的更新答案。
-
如果我不这样做,自定义视图的背景是白色的......这就是我问的原因。一开始我没有设置,但是看到背景是白色后,猜想我需要为自定义视图设置透明度