簡體   English   中英

帶圓角的UIView有黑色背景

[英]UIView with round corners has black background

我正在嘗試創建一個帶圓角的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

RENDERED UI

的UIView

編輯

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將您選擇的背景顏色分配給新創建的視圖。 多數民眾贊成你的問題解決了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM