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

时间:2018-01-23 08:37:18

标签: ios swift uibutton subviews

我有一个工作登录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.
    }
}

2 个答案:

答案 0 :(得分:5)

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

self.view.addSubview(imageView)

到此:

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

然后删除将按钮作为imageView

的子视图放置的代码行

这会将图像放在按钮下方,因此它会按照您的需要显示,按钮将继续工作。

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

enter image description here

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

答案 1 :(得分:0)

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