Ios 带有圆角的UIView具有黑色背景
我试图创建一个带有圆角的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:
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,它不是透明背景。这听起来像是你在匹配背景的颜色,这是不完全一样的事情。相反,请尝试将背景色设置为。请清除。