ios – Important view’s border in my customized UIview class is on prime of one other view

ios – Important view’s border in my customized UIview class is on prime of one other view


Screenshot of my challenge

I’ve a problem the place the principle view’s border of a customized UIview class I’ve is happening prime of the rankbadgeview inside this practice class. I can not for the lifetime of me work out why. I’ve tried bringing the rankbadgeview subview to the entrance together with the uilabel within it and nonetheless would not work. Can anyone assist me spot the difficulty?

class LeaderboardCircleView: UIView {
    non-public let mainLabel = UILabel()
    let rankBadgeView = UIView()
    non-public let rankLabel = UILabel()
    non-public let scoreLabel = UILabel()
    
    init(mainText: String, rankText: String, backgroundColor: UIColor, rating: Int) {
        tremendous.init(body: .zero)
        setupViews()
        configure(mainText: mainText, rankText: rankText, backgroundColor: backgroundColor, rating: rating)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been applied")
    }


    non-public func updateScoreLabelWith(rating: Int) {
        let trophyAttachment = NSTextAttachment()
        trophyAttachment.picture = UIImage(systemName: "trophy.fill")?.withTintColor(.systemYellow, renderingMode: .alwaysOriginal)
        trophyAttachment.bounds = CGRect(x: 0, y: -2, width: 16, top: 16)
        
        let trophyString = NSAttributedString(attachment: trophyAttachment)
        let scoreString = NSAttributedString(string: " (rating) pts", attributes: [
            .font: AppStyle.shared.headerFont(size: 12),
            .foregroundColor: UIColor.darkGray
        ])
        
        let fullScoreText = NSMutableAttributedString()
        fullScoreText.append(trophyString)
        fullScoreText.append(scoreString)
        
        scoreLabel.attributedText = fullScoreText
    }

    
    non-public func setupViews() {
        layer.borderWidth = 2
        layer.borderColor = AppStyle.shared.primaryColor?.cgColor
        translatesAutoresizingMaskIntoConstraints = false
        clipsToBounds = false
        
        mainLabel.translatesAutoresizingMaskIntoConstraints = false
        mainLabel.textAlignment = .middle
        mainLabel.adjustsFontSizeToFitWidth = true
        mainLabel.minimumScaleFactor = 0.5
        mainLabel.numberOfLines = 2
        mainLabel.font = AppStyle.shared.headerFont(dimension: 18)
        mainLabel.textColor = .white
        addSubview(mainLabel)
        
        rankBadgeView.translatesAutoresizingMaskIntoConstraints = false
        rankBadgeView.clipsToBounds = true
        rankBadgeView.backgroundColor = AppStyle.shared.primaryColor
        rankBadgeView.layer.zPosition = 1  // Sends it behind the border

        addSubview(rankBadgeView)
        
        rankLabel.translatesAutoresizingMaskIntoConstraints = false
        rankLabel.textAlignment = .middle
        rankLabel.font = AppStyle.shared.headerFont(dimension: 12)
        rankLabel.textColor = .white
        rankBadgeView.addSubview(rankLabel)
        
        scoreLabel.translatesAutoresizingMaskIntoConstraints = false
        scoreLabel.textAlignment = .middle
        scoreLabel.adjustsFontSizeToFitWidth = true
        scoreLabel.minimumScaleFactor = 0.5
        scoreLabel.numberOfLines = 1
        addSubview(scoreLabel)
        
        NSLayoutConstraint.activate([
            mainLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10),
            mainLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -10),
            mainLabel.centerXAnchor.constraint(equalTo: centerXAnchor),
            mainLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
            
            rankBadgeView.centerXAnchor.constraint(equalTo: centerXAnchor),
            rankBadgeView.bottomAnchor.constraint(equalTo: bottomAnchor, constant: 12),
            rankBadgeView.widthAnchor.constraint(equalToConstant: 30),
            rankBadgeView.heightAnchor.constraint(equalToConstant: 30),
            
            rankLabel.centerXAnchor.constraint(equalTo: rankBadgeView.centerXAnchor),
            rankLabel.centerYAnchor.constraint(equalTo: rankBadgeView.centerYAnchor),
            
            scoreLabel.topAnchor.constraint(equalTo: rankBadgeView.bottomAnchor, constant: 4),
            scoreLabel.centerXAnchor.constraint(equalTo: centerXAnchor)
        ])
        
        bringSubviewToFront(rankBadgeView)
        rankBadgeView.bringSubviewToFront(scoreLabel)
    }
    
    non-public func configure(mainText: String, rankText: String, backgroundColor: UIColor, rating: Int) {
        mainLabel.textual content = mainText
        self.backgroundColor = backgroundColor
        rankLabel.textual content = rankText
        
        let trophyAttachment = NSTextAttachment()
        trophyAttachment.picture = UIImage(systemName: "trophy.fill")?.withTintColor(.systemYellow, renderingMode: .alwaysOriginal)
        trophyAttachment.bounds = CGRect(x: 0, y: -2, width: 16, top: 16)
        
        let trophyString = NSAttributedString(attachment: trophyAttachment)
        let scoreString = NSAttributedString(string: " (rating) pts", attributes: [
            .font: AppStyle.shared.headerFont(size: 12),
            .foregroundColor: UIColor.darkGray
        ])
        
        let fullScoreText = NSMutableAttributedString()
        fullScoreText.append(trophyString)
        fullScoreText.append(scoreString)
        
        scoreLabel.attributedText = fullScoreText
    }
    
    override func layoutSubviews() {
        tremendous.layoutSubviews()
        
        layer.cornerRadius = bounds.width / 2
        rankBadgeView.layer.cornerRadius = rankBadgeView.bounds.width / 2
        
        layer.shadowColor = UIColor.black.cgColor
        layer.shadowOpacity = 0.2
        layer.shadowRadius = 6.0
        layer.shadowOffset = CGSize(width: 0, top: 4)
        layer.masksToBounds = false
        
        rankBadgeView.layer.shadowColor = UIColor.black.cgColor
        rankBadgeView.layer.shadowOpacity = 0.2
        rankBadgeView.layer.shadowRadius = 4.0
        rankBadgeView.layer.shadowOffset = CGSize(width: 0, top: 2)
        rankBadgeView.layer.masksToBounds = false
    }
    
    func replace(mainText: String, rankText: String, backgroundColor: UIColor, rating: Int) {
        configure(mainText: mainText, rankText: rankText, backgroundColor: backgroundColor, rating: rating)
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *