Swift inheritance carrying over constraints

78 Views Asked by At

In my swift code below I am attempting to use inheritance. When I go to class middle the constraints on the initial view controller are still being applied. Even though in middle class I attempt to attach new constraints to right button they are not being applied. You can see what is going wrong in the photo below.

enter image description here

import UIKit

class ViewController: UIViewController {
    
    var right = UIButton()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        [right].forEach{
            $0.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview($0)
            $0.backgroundColor = UIColor(
                red: .random(in: 0.0...1),
                green: .random(in: 0.9...1),
                blue: .random(in: 0.7...1),
                alpha: 1
            )
        }
        
        NSLayoutConstraint.activate([

            right.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            right.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            right.heightAnchor.constraint(equalTo: view.heightAnchor,multiplier: 0.1),
            right.widthAnchor.constraint(equalTo: view.widthAnchor,multiplier: 1),
            
            
        ])
        right.setTitle(">", for: .normal)
        right.addTarget(self, action: #selector(nexte), for: .touchDown)
    }
    
    
    @objc func nexte(){
        
        let vc = middle()
        vc.modalPresentationStyle = .overCurrentContext // actually .fullScreen would be better
        self.present(vc, animated: true)
        
    }
    
}


class middle : ViewController{
    
    var leftbtn = UIButton()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        [leftbtn].forEach{
            $0.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview($0)
            $0.backgroundColor = UIColor(
                red: .random(in: 0.0...1),
                green: .random(in: 0.9...1),
                blue: .random(in: 0.7...1),
                alpha: 1
            )
            
        }
        
        NSLayoutConstraint.activate([
            
            leftbtn.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            leftbtn.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            leftbtn.heightAnchor.constraint(equalTo: view.heightAnchor,multiplier: 0.1),
            leftbtn.widthAnchor.constraint(equalTo: view.widthAnchor,multiplier: 0.5 ),
            
            right.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            right.leadingAnchor.constraint(equalTo: leftbtn.trailingAnchor),
            right.heightAnchor.constraint(equalTo: view.heightAnchor,multiplier: 0.1),
            right.widthAnchor.constraint(equalTo: view.widthAnchor,multiplier: 0.5 ),
            
        ])
        
    }
}
1

There are 1 best solutions below

4
matt On

You are saying super.viewDidLoad() in Middle. super is ViewController so ViewController's viewDidLoad is executed too. Thus "When I go to class middle the constraints on the initial view controller are still being applied".

You should rethink your whole architecture here.