简体   繁体   English

Swift inheritance 携带约束

[英]Swift inheritance carrying over constraints

In my swift code below I am attempting to use inheritance.在下面的 swift 代码中,我尝试使用 inheritance。 When I go to class middle the constraints on the initial view controller are still being applied.当我 go 到 class 中间时,初始视图 controller 的约束仍在应用中。 Even though in middle class I attempt to attach new constraints to right button they are not being applied.即使在中间 class 我尝试将新的约束附加到右键,它们也没有被应用。 You can see what is going wrong in the photo below.您可以在下面的照片中看到出了什么问题。

在此处输入图像描述

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 ),
            
        ])
        
    }
}

You are saying super.viewDidLoad() in Middle.你在中间说super.viewDidLoad() super is ViewController so ViewController's viewDidLoad is executed too. super是 ViewController 所以 ViewController 的viewDidLoad也会被执行。 Thus "When I go to class middle the constraints on the initial view controller are still being applied".因此,“当我 go 到 class 中间时,初始视图 controller 的约束仍在应用中”。

You should rethink your whole architecture here.你应该在这里重新考虑你的整个架构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM