添加为背景图像的子视图后按钮不起作用

Buttons are not working after adding as subviews to background image

我有一个可用的登录名 Viewcontroller 但是当我添加背景图像时按钮的功能不起作用,没有语法错误。当我仅作为背景图片添加时,每个按钮都可以正常工作,但是当我将按钮作为子视图添加时,按钮不起作用。

这是我的 VC:

import UIKit
import CoreData
import Foundation

@available(iOS 11.0, *)
class ViewController: UIViewController {

    var imageView = UIImageView()

    @IBOutlet weak var signupButton: UIButton!
    @IBOutlet weak var directButton: UIButton!

    @IBOutlet weak var emailText: UITextField!
    @IBOutlet weak var passText: UITextField!

    @IBOutlet weak var loginButton: UIButton!
    @IBAction func loginAction(_ sender: Any) {

            let appDel = UIApplication.shared.delegate as! AppDelegate
            let context = appDel.persistentContainer.viewContext
            var isMatched = false
            let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Users")
            request.returnsObjectsAsFaults = false
            do {
                let results = try! context.fetch(request)
                if(results.count > 0){
                    for result in results as! [NSManagedObject]
                    {
                        if  emailText.text == result.value(forKey: "username") as? String && passText.text == result.value(forKey: "password") as? String {
                            print(emailText.text!, passText.text!)
                           // usernameGlobal = self.emailText.text!
                            let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle : nil)
                            let desController = mainStoryBoard.instantiateViewController(withIdentifier: "AddViewController") as! AddViewController
                            let newFrontViewController = UINavigationController.init(rootViewController:desController)
                            revealViewController().pushFrontViewController(newFrontViewController, animated: true)
                            isMatched = true
                            break
                        }
                    }

                    if !isMatched {
                        let alertController = UIAlertController(title: "Oops!", message: "Incorrect username or password", preferredStyle: .alert)
                        let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
                        alertController.addAction(defaultAction)
                        present(alertController, animated: true, completion: nil)
                    }
                }
            }
    }



    @IBAction func directAction(_ sender: Any) {


        let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle : nil)
        let desController = mainStoryBoard.instantiateViewController(withIdentifier: "AddViewController") as! AddViewController
        let newFrontViewController = UINavigationController.init(rootViewController:desController)
        revealViewController().pushFrontViewController(newFrontViewController, animated: true)


    }



    @IBAction func signupSegue(_ sender: Any) {

        let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle : nil)
        let desController = mainStoryBoard.instantiateViewController(withIdentifier: "SignupViewController") as! SignupViewController
        let newFrontViewController = UINavigationController.init(rootViewController:desController)
        revealViewController().pushFrontViewController(newFrontViewController, animated: true)
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        imageView = UIImageView(frame: UIScreen.main.bounds)
        imageView.image = #imageLiteral(resourceName: "dsplash")
        imageView.contentMode = .scaleAspectFill
         imageView.alpha = 0.5

        self.view.addSubview(imageView)

        self.imageView.addSubview(emailText)
        self.imageView.addSubview(passText)
        self.imageView.addSubview(loginButton)
        self.imageView.addSubview(signupButton)
        self.imageView.addSubview(directButton)

        loginButton.backgroundColor = UIColor.blue
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

不要将按钮添加为 imageView 的子视图,它们由故事板布局。而只是更改这行代码:

self.view.addSubview(imageView)

对此:

self.view.insertSubview(imageView, at: 0)

然后删除将按钮作为 imageView 的子视图的代码行。

这会将图像放在按钮下方,因此它看起来就像您想要的那样,并且按钮将继续工作。

或者更好的是,通过故事板添加 imageView(我喜欢保持一致)。要将其保留为背景图像,只需将视图控制器场景中的 imageView 拖到子视图的顶部,这样它就是视图的第一个子视图 - 这将使视图的所有其余部分都出现在它之前:

或者,如果您有充分的理由将按钮保留为 imageView 子视图,请设置 imageView.userInteractionEnabled = true 以使其与作为 imageView.[=20= 的子视图的按钮一起使用]

图像视图用户交互启用默认为否。你需要改变它。使用 [imageView setUserInteractionEnabled: Yes] ;