【发布时间】:2020-04-12 22:36:50
【问题描述】:
我有一个故事板,其中设置了三个 UIView 并连接到故事板的 UIViewController。
@IBOutlet weak var availableView: UIView!
@IBOutlet weak var maybeAvailableView: UIView!
@IBOutlet weak var notAvailableView: UIView!
我有两个函数可以将 UILabel 和 CAGradientLayer 添加到每个 UIView 中。这是用于添加 UILabel 的。
func setUpLabels(view1: UIView, view2: UIView, view3: UIView) {
let availableLabel = UILabel()
let maybeAvailableLabel = UILabel()
let notAvailableLabel = UILabel()
availableLabel.text = "Available"
maybeAvailableLabel.text = "Maybe Available"
notAvailableLabel.text = "Not Available"
availableLabel.backgroundColor = .clear
maybeAvailableLabel.backgroundColor = .clear
notAvailableLabel.backgroundColor = .clear
availableLabel.font = UIFont.systemFont(ofSize: 17, weight: UIFont.Weight(rawValue: 10))
maybeAvailableLabel.font = UIFont.systemFont(ofSize: 17, weight: UIFont.Weight(rawValue: 10))
notAvailableLabel.font = UIFont.systemFont(ofSize: 17, weight: UIFont.Weight(rawValue: 10))
availableLabel.textColor = .black
maybeAvailableLabel.textColor = .black
notAvailableLabel.textColor = .black
availableLabel.frame = view1.bounds
maybeAvailableLabel.frame = view2.bounds
notAvailableLabel.frame = view3.bounds
view1.addSubview(availableLabel)
view2.addSubview(maybeAvailableLabel)
view3.addSubview(notAvailableLabel)
}
这个是用来添加渐变的。
func setGradientBackground(colorTop: UIColor, colorBottom: UIColor, view: UIView) {
let gradientLayer = CAGradientLayer()
gradientLayer.colors = [colorBottom.cgColor, colorTop.cgColor]
gradientLayer.startPoint = CGPoint(x: 1, y: 1)
gradientLayer.endPoint = CGPoint(x: 0, y: 0)
gradientLayer.locations = [0, 1]
gradientLayer.frame = view.bounds
view.layer.insertSublayer(gradientLayer, at: 0)
}
这就是我在 viewDidLayoutSubviews 中所说的
availableView.layer.masksToBounds = true
maybeAvailableView.layer.masksToBounds = true
notAvailableView.layer.masksToBounds = true
setGradientBackground(colorTop: darkBlue, colorBottom: .systemIndigo, view: notAvailableView)
setGradientBackground(colorTop: .systemRed, colorBottom: .systemOrange, view: maybeAvailableView)
setGradientBackground(colorTop: .systemBlue, colorBottom: .systemTeal, view: availableView)
setUpLabels(view1: availableView, view2: maybeAvailableView, view3: notAvailableView)
但是,viewController 或调试层次视图中没有显示任何内容。我从其他问题中尝试了很多东西,但没有任何改变。
【问题讨论】:
标签: ios swift storyboard uilabel cagradientlayer