Sunday, July 19, 2026
HomeiOS Developmentios - Can I add views to the identical hierarchy in XCode...

ios – Can I add views to the identical hierarchy in XCode whereas splitting them into capabilities?

[ad_1]

I’m new to XCode, and I’m making an attempt to render a unique subview primarily based on a situation. To attain this, I created two separate recordsdata that return a UIView:

That is the occupied area file:

import UIKit

func createOccupiedSpaceView(colour: UIColor, playerName: String) -> UIView {
    let occupiedView = UIView()
    occupiedView.backgroundColor = colour
    
    return occupiedView
}

and that is the unoccupied area file:

func createUnoccupiedView(colour: UIColor) -> UIView {
    let unoccupiedView = UIView()
    unoccupiedView.backgroundColor = colour
    
    let addLabel = UILabel()
    addLabel.textual content = "ADD NEW PLAYER"
    
    addLabel.anchor(prime: unoccupiedView.topAnchor, main: unoccupiedView.leadingAnchor, backside: unoccupiedView.bottomAnchor, trailing: unoccupiedView.trailingAnchor, dimension: CGSize(width: 20, top: 20))
    
    [addLabel].forEach({unoccupiedView.addSubview($0)})
    
    return unoccupiedView
}

I’ve additionally created this extension to make my constraints simpler

extension UIView {
    func anchor(prime: NSLayoutYAxisAnchor?, main: NSLayoutXAxisAnchor?, backside: NSLayoutYAxisAnchor?, trailing: NSLayoutXAxisAnchor?, padding: UIEdgeInsets = .zero, dimension: CGSize = .zero) {
        
        translatesAutoresizingMaskIntoConstraints = false
        
        if let prime = prime {
            topAnchor.constraint(equalTo: prime, fixed: padding.prime).isActive = true
        }
        
        if let main = main {
            leadingAnchor.constraint(equalTo: main, fixed: padding.left).isActive = true
        }
        
        if let backside = backside {
            bottomAnchor.constraint(equalTo: backside, fixed: -padding.backside).isActive = true
        }
        
        if let trailing = trailing {
            trailingAnchor.constraint(equalTo: trailing, fixed: -padding.proper).isActive = true
        }
        
        if dimension.width != 0 {
            widthAnchor.constraint(equalToConstant: dimension.width).isActive = true
        }
        
        if dimension.top != 0 {
            heightAnchor.constraint(equalToConstant: dimension.top).isActive = true
        }

    }

Right here is the view controller that runs the code:

import UIKit

class LobbyVC: UIViewController {
    personal let recreation: Recreation
    
    init?(coder: NSCoder, recreation: Recreation) {
        self.recreation = recreation

        tremendous.init(coder: coder)
    }
    
    required init?(coder: NSCoder) {
        fatalError("Use `init(coder:recreation:)` to initialize an `LobbyVC` occasion.")
    }
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        
        // Get acceptable participant area views
        let VALID_PLAYER_POSITIONS = [1, 2, 3, 4]
        var playerDictionary = [Int: UIView]()

        for place in VALID_PLAYER_POSITIONS {
            playerDictionary[position] = getCorrectView(place: place)
        }

    }
    
    func getCorrectView(place: Int) -> UIView{
        if let participant = self.recreation.gamers.filter({$0.place == place}).first {
            return createOccupiedSpaceView(colour: .inexperienced, playerName: participant.title)
        } else {
            return createUnoccupiedView(colour: .inexperienced)
        }
    }
}

I consider that including constraints to my unoccupied area is creating points for me, however I’m uncertain cut up up my views and make my code cleaner whereas protecting my views in the identical hierarchy. Every time that I attempt to run my code, I get this error:

Thread 1: "Unable to activate constraint with anchors <NSLayoutYAxisAnchor:0x600001fbc500 "UILabel:0x12fd12fa0.prime"> and <NSLayoutYAxisAnchor:0x600001fbc040 "UIView:0x12fd07f70.prime"> as a result of they haven't any widespread ancestor.  Does the constraint or its anchors reference objects in numerous view hierarchies?  That is unlawful." 

Any assistance on fixing could be appreciated!

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments