将UILabel添加到自定义UIView,但不显示在UIImage视图上

时间:2019-12-09 13:43:52

标签: ios swift uiview uilabel

因此,我需要一些将包含一些元素的自定义视图。我希望能够以编程方式设置所有视图,以习惯于不使用界面生成器。

我已添加图像,但无法使标签显示在UIImageview上方。我已经玩了一些代码,但似乎无法使它正常工作。我肯定这一定是我错过的傻事,但可以弄清楚。以下是自定义视图的代码。在视图控制器文件中为每个自定义视图添加了图像和标签文本。

import UIKit

class IntroButton: UIView {    
    let mainImage = UIImageView()
    let titalBackground = UIView()
    let mainTital = UILabel()

    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(mainTital)
        addSubview(mainImage)
        addSubview(titalBackground)
        setUpTital()
        setUpImage()
        setUpButton()

    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        addSubview(mainTital)
        addSubview(mainImage)
        addSubview(titalBackground)
        setUpTital()
        setUpButton()
        setUpImage()

    }

    func setUpButton() {
        //basic view setup
        layer.cornerRadius = 20
        layer.shadowColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
        layer.shadowRadius = 20
        layer.shadowOpacity = 100
        layer.shadowOffset = CGSize(width: 10, height: 10)
        layer.masksToBounds = true
    }

    //helps with adding image and content to view
    override var intrinsicContentSize: CGSize{
        return CGSize(width: 200, height: 300)

    }

    func setUpImage() {
        //adding image to the main view and image constraints
        mainImage.contentMode = .scaleAspectFill
        mainImage.translatesAutoresizingMaskIntoConstraints = false
        mainImage.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
        mainImage.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
        mainImage.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
        mainImage.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
        mainImage.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        mainImage.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
    }

    func setUpTital() {
        mainTital.font = UIFont(name: "Futura", size: 25)
        mainTital.textColor = .white
        mainTital.translatesAutoresizingMaskIntoConstraints = false
        mainTital.heightAnchor.constraint(equalToConstant: 100).isActive = true
        mainTital.widthAnchor.constraint(equalToConstant: self.frame.width).isActive = true
        mainTital.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
        mainTital.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        mainTital.textAlignment = .left
        mainTital.clipsToBounds = true
    }
}

2 个答案:

答案 0 :(得分:1)

您最多将标签向后添加更改顺序

addSubview(mainImage)
addSubview(titalBackground) 
addSubview(mainTital) /// last 1 appears at most top 

答案 1 :(得分:1)

将元素添加到视图时,它们按照添加顺序进行分层。

更改此:

addSubview(mainTital)
addSubview(mainImage)
addSubview(titalBackground)

对此:

// add main image
addSubview(mainImage)
// add tital background *on top of* main image
addSubview(titalBackground)
// add main tital *on top of* tital background
addSubview(mainTital)