-1

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 Answers1

0

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • How should I change it what do recommend I am just constraining 2 things here. – silly milly Jan 18 '22 at 02:54
  • Why do you have one view controller inheriting from another? As I said, rethink this. – matt Jan 18 '22 at 14:40
  • I am trying to keep the same objects in all of the classes. Except the first class will just have a right page button and the rest of the classes will have a left and right page button – silly milly Jan 18 '22 at 19:33
  • Well I've explained the cause of the issue. Take it or leave it. – matt Jan 18 '22 at 20:28