【问题标题】:UIView with round corners has black background带圆角的 UIView 具有黑色背景
【发布时间】:2018-06-05 16:02:24
【问题描述】:

我正在尝试创建一个带有圆角的 UIView,圆角呈现完美,但视图后面有一个奇怪的黑色背景。如何摆脱黑色背景?

代码

import UIKit
import PlaygroundSupport

// The background rectange for main view
let backgroundRectangle = CGRect(x: 0, y: 0, width: 500, height: 500)
// Main view
let mainView = UIView(frame: backgroundRectangle)
// Color of the main view
mainView.backgroundColor = .darkGray
// Corner radius
mainView.layer.cornerRadius = 50
mainView.layer.masksToBounds = false
// Present the view to Playground's live view
PlaygroundPage.current.liveView = mainView

渲染的用户界面

编辑

import UIKit
import PlaygroundSupport

// Holder rectangle
let holderRectangle = CGRect(x: 0, y: 0, width: 500, height: 500)
// Holder view
let holderView = UIView(frame: holderRectangle)
// The background color of the holder view is clear
holderView.backgroundColor = .clear

// Main rectangle
let mainRectangle = CGRect(x: 0, y: 0, width: 500, height: 500)
// Main view
let mainView = UIView(frame: mainRectangle)
// The background color of the main view is black
mainView.backgroundColor = .white
// Create a corner radius for the main view
mainView.layer.cornerRadius = 30
mainView.layer.masksToBounds = false

// Make the main view a subview of the holder view
holderView.addSubview(mainView)

// Present the holder view in the live view
PlaygroundPage.current.liveView = holderView

【问题讨论】:

  • mainView 后面的视图是黑色的。将mainView 放在其他颜色不同的视图中,您将看到不同的颜色。你的mainView 很好。

标签: ios swift uiview


【解决方案1】:

发生这种情况是因为在您的mainView 后面没有其他视图。请在mainView 后面添加一个视图或将您的mainView 嵌入UIView 为新创建的视图分配您选择的背景颜色。你的问题就这样解决了。

【讨论】:

  • 用这个holderView.backgroundColor = .white替换这个holderView.backgroundColor = .clear
  • 解决了!谢谢。对于那些使用 Swift Playground 并希望主视图后面的视图具有透明背景的用户,您可以将颜色设置为 RGB 值:247、247、247。
  • @NikhilRaghavendra 不是透明背景。听起来您正在匹配背景的颜色,这并不完全相同。相反,请尝试将背景颜色设置为 .clear
猜你喜欢
  • 1970-01-01
  • 2014-05-11
  • 2013-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-02
  • 2023-03-19
相关资源
最近更新 更多