【问题标题】:Custom component background transparent自定义组件背景透明
【发布时间】: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.
}
}

【问题讨论】:

  • 请添加背景图片的代码。
  • 设置图案后不要再设置背景色。请参阅我的更新答案。
  • 如果我不这样做,自定义视图的背景是白色的......这就是我问的原因。一开始我没有设置,但是看到背景是白色后,猜想我需要为自定义视图设置透明度

标签: ios swift


【解决方案1】:

设置颜色为 UIColor.clearColor()

【讨论】:

  • 嗯。你有更多细节吗?视图是在 IB 中创建的吗?并且 UIScrollView 实际上是否位于(更高的 z 值)之上,而不是您试图使其透明的视图?
  • 是的,自定义视图是在 xib 中创建的,而 ScrollView 位于我尝试使其透明的视图后面。
  • 有趣。我想测试一下。你能去Xcode -> About Xcode,告诉我你用的是什么版本吗?
  • 另外,您是否在代码中指定了背景颜色,并在 IB 中指定了背景图像?背景图像实际上是分配给 backgroundColor 属性的值。你试过去掉 IB 中的图片背景吗?
  • 感谢代码!我正在尝试复制您的问题。同时,尝试在 Your ViewController 类中设置背景颜色,看看我的方法是否有效。为此: 转到 Main.Storyboard Option-click ViewController.Swift Control-drag 从您的自定义视图到 .Swift 文件中,以创建一个命名的插座。在 viewDidLoad 方法中,将该视图的背景颜色设置为 UIColor.clearColor() 我没有使用 .xib,但这对我有用。
【解决方案2】:

为滚动视图背景使用清晰的颜色

self.scrollView.backgroundColor = UIColor.clearColor()

使用图案图像设置颜色后,您无需再次设置视图的背景颜色。如果再次设置背景颜色,图案图像将被移除。

【讨论】:

  • 我在自定义视图的 xib 中设置背景颜色(我需要透明的颜色),而不是在 Main.storyboard 的 ViewController 中。图案图像位于 Main.storyboard 的 ViewController 中。
  • 您的要求似乎不清楚。考虑用更多细节更新您的问题,使其更易于理解。
【解决方案3】:

作为一种变体,您可以使用UIWebView 以便使用 HTML 代码在 HTML 代码中设置透明度。

要实现具有透明背景的 UIWebView,您只需:

  1. UIWebView 的backgroundColor 属性设置为[UIColor clearColor]
  2. 在 HTML 中使用 UIWebView 的内容。
  3. UIWebView 的 opaque 属性设置为 NO

PS:摘自Using transparent background color in UIView by HTML code

【讨论】:

  • 这对我来说看起来很糟糕。我不需要使用 UIWebView 来使背景透明……我怀疑 Apple 会让我发布它。
  • @Fustigador 无论如何,它是如何做到这一点的方式(奇怪的方式,但无论如何)。我会尝试找到另一种解决方案
【解决方案4】:

我遇到了同样的问题(同样的教程),我是如何解决的:

  1. 在xibSetup方法中添加:

    view.backgroundColor = UIColor(white: 0.9, alpha: 1.0) // 或任何颜色

  2. 在 UIViewController 中创建:

    让 noDataView = NoDataUIView(frame: self.view.frame)

    view.addSubview(noDataView)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-22
    • 1970-01-01
    • 2014-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多