Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 带有圆角的UIView具有黑色背景_Ios_Swift_Uiview - Fatal编程技术网

Ios 带有圆角的UIView具有黑色背景

Ios 带有圆角的UIView具有黑色背景,ios,swift,uiview,Ios,Swift,Uiview,我试图创建一个带有圆角的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:

我试图创建一个带有圆角的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
呈现的UI

编辑

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
嵌入
UIView
为新创建的视图指定您选择的背景色。您的问题解决了。

主视图后面的视图为黑色。将
mainView
放入其他具有不同颜色的视图中,您将看到不同的颜色。您的
main视图
正常。将此
holderView.backgroundColor=.clear
替换为此
holderView.backgroundColor=.white
已解决!谢谢对于使用Swift游乐场并希望主视图后面的视图具有透明背景的用户,可以将颜色设置为RGB值:247、247、247。@NikhilRaghavendra,它不是透明背景。这听起来像是你在匹配背景的颜色,这是不完全一样的事情。相反,请尝试将背景色设置为
。请清除