UIButton 和 UITextField 作为 UIViewController 的一部分不响应

巫师

我是 swift 的新手,我有这个代码,它是UIViewController用于UITextField和的UIButton

import Foundation
import UIKit

class ManualTagController: UIViewController, UIGestureRecognizerDelegate, UITextFieldDelegate {
    let addTagButton = UIButton()
    let tagTextfield = UITextField()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        self.view.layer.cornerRadius = self.view.frame.width/80

        self.addTagButton.setTitle("TAG", for: .normal)
        self.addTagButton.backgroundColor = UIColor(red: 200.0/255, green: 17.0/255, blue: 57.0/255, alpha: 0.75)
        self.addTagButton.addTarget(self, action: #selector(addTag), for: .touchUpInside)
        self.addTagButton.layer.masksToBounds = true
        self.addTagButton.clipsToBounds = true
        view.addSubview(addTagButton)

        self.tagTextfield.placeholder = "# or 'hey siri'"
        self.tagTextfield.alpha = 1
        self.tagTextfield.font = UIFont.systemFont(ofSize: 52)
        self.tagTextfield.borderStyle = UITextBorderStyle.roundedRect
        self.tagTextfield.autocorrectionType = UITextAutocorrectionType.no
        self.tagTextfield.keyboardType = UIKeyboardType.default
        self.tagTextfield.returnKeyType = UIReturnKeyType.done
        self.tagTextfield.clearButtonMode = UITextFieldViewMode.whileEditing;
        self.tagTextfield.contentVerticalAlignment = UIControlContentVerticalAlignment.center
        self.tagTextfield.delegate = self
        view.addSubview(tagTextfield)
    }

    override func viewDidLayoutSubviews() {
        self.addTagButton.frame = CGRect(x: 10.0, y: self.view.frame.height - 77.0, width: self.view.frame.width - 20.0, height:72.0)
        self.addTagButton.layer.cornerRadius = self.addTagButton.frame.width/80
        self.tagTextfield.frame = CGRect(x: 10.0, y: 5.0, width: self.view.frame.width - 20.0, height:72.0)
    }

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

    @objc private func addTag() {
        //Database service code here...
        print("in addTag .........")
        //This will go where service is done and it is successful
    }

    internal func textFieldDidBeginEditing(_ textField: UITextField) {    //delegate method
        //
    }

    internal func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {  //delegate method
        return false
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {   //delegate method
        textField.resignFirstResponder()

        return true
    }
}

在我的主要内容中UIViewController- 它会像这样加载,点击按钮。它加载得很好 - 我只是展示了我在layer这里是如何做的,以及它是如何初始化的:

@objc private func openManualTag() {
        let frameHeightAdjusted = self.view.frame.height - 5/8 * self.view.frame.height
        let manualTag = ManualTagController()
        manualTag.view.layer.shadowColor = UIColor.black.cgColor
        manualTag.view.layer.shadowOpacity = 0.3
        manualTag.view.layer.shadowOffset = CGSize.zero
        manualTag.view.layer.shadowRadius = 5
        manualTag.view.layer.masksToBounds = true
        manualTag.view.clipsToBounds = false
        manualTag.view.layer.shadowPath = UIBezierPath(rect: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 159)).cgPath
        manualTag.view.tag = 1
        manualTag.view.frame = CGRect(x: 0, y: frameHeightAdjusted, width: self.view.frame.width, height: 159.0)
        manualTag.view.transform = CGAffineTransform(translationX: 0, y: -frameHeightAdjusted - 159)

        DispatchQueue.main.async {
            self.view.addSubview(manualTag.view)
            UIView.animate(withDuration: 0.5, animations: {
                manualTag.view.transform = .identity
            })
        }
    }

问题是既不工作UIButton也不行UITextField......无论如何不要回应敲击它们。我究竟做错了什么?

更新

发布所有ViewController,我的主视图控制器,以防万一:

import UIKit
import GoogleMaps


class ViewController: UIViewController, CLLocationManagerDelegate {
    let locationManager = CLLocationManager()
    var center = CLLocationCoordinate2D()
    let tagButton = TagButton()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tagButton.addTarget(self, action: #selector(openManualTag), for: .touchUpInside)
        self.tagButton.backgroundColor = UIColor(red: 200.0/255, green: 17.0/255, blue: 57.0/255, alpha: 0.5)
        self.tagButton.layer.shadowColor = UIColor.black.cgColor
        self.tagButton.layer.shadowPath = UIBezierPath(roundedRect: self.tagButton.bounds, cornerRadius: self.tagButton.frame.width/2).cgPath
        self.tagButton.layer.shadowOffset = CGSize.zero
        self.tagButton.layer.shadowOpacity = 0.3
        self.tagButton.layer.shadowRadius = 5
        self.tagButton.layer.masksToBounds = true
        self.tagButton.clipsToBounds = false
        self.tagButton.layer.borderWidth = 5
        self.tagButton.layer.borderColor = UIColor.white.cgColor
        view.addSubview(self.tagButton);
    }

    override func viewDidLayoutSubviews() {
        //let tagButton = view.viewWithTag(0)
        self.tagButton.frame = CGRect(x: self.view.frame.width/2 - 36, y: self.view.frame.height - 0.2 * self.view.frame.height, width: 72.0, height: 72.0)
    }

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

    override func loadView() {
        let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
        let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
        view = mapView
    }

    func addMarker(tag: String) {
        self.locationManager.startUpdatingLocation();
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: self.center.latitude, longitude: self.center.longitude)
        marker.title = tag
        //marker.snippet = //USERNAME
        DispatchQueue.main.async {
            marker.map = self.view as? GMSMapView
        }
    }

    @objc private func openManualTag() {
        let frameHeightAdjusted = self.view.frame.height - 5/8 * self.view.frame.height
        let manualTag = ManualTagController()
        manualTag.view.layer.shadowColor = UIColor.black.cgColor
        manualTag.view.layer.shadowOpacity = 0.3
        manualTag.view.layer.shadowOffset = CGSize.zero
        manualTag.view.layer.shadowRadius = 5
        manualTag.view.layer.masksToBounds = true
        manualTag.view.clipsToBounds = false
        manualTag.view.layer.shadowPath = UIBezierPath(rect: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 159)).cgPath
        manualTag.view.tag = 1
        manualTag.view.frame = CGRect(x: 0, y: frameHeightAdjusted, width: self.view.frame.width, height: 159.0)
        manualTag.view.transform = CGAffineTransform(translationX: 0, y: -frameHeightAdjusted - 159)

        let transparencyButton = UIButton(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
        transparencyButton.backgroundColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha:0.4);
        transparencyButton.addTarget(self, action: #selector(dismissHelper(sender:)), for: .touchUpInside)

        DispatchQueue.main.async {
            self.addChildViewController(manualTag)
            self.view.addSubview(manualTag.view)
            manualTag.didMove(toParentViewController: self)

            UIView.animate(withDuration: 0.5, animations: {
                manualTag.view.transform = .identity
            })

            self.view.insertSubview(transparencyButton, belowSubview: manualTag.view)
        }
    }

    @objc func dismissHelper(sender: UIButton)
    {
        self.view.viewWithTag(1)?.removeFromSuperview()
        sender.isHidden = true;
    }

    override func viewDidAppear(_ animated: Bool) {
        self.locationManager.delegate = self
        self.locationManager.requestWhenInUseAuthorization()
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.startUpdatingLocation()
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print("Error" + error.localizedDescription)
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let userLocation = locations.last
        self.center = CLLocationCoordinate2D(latitude: userLocation!.coordinate.latitude, longitude: userLocation!.coordinate.longitude)

        let mapView = view as! GMSMapView
        DispatchQueue.main.async {
            mapView.camera = GMSCameraPosition.camera(withLatitude: self.center.latitude, longitude: self.center.longitude, zoom: 14.0)
        }

        print("Latitude :- \(userLocation!.coordinate.latitude)")
        print("Longitude :-\(userLocation!.coordinate.longitude)")

        self.locationManager.stopUpdatingLocation()
    }
}
鲁斯兰·谢列布里亚科夫

当您将其他视图控制器的视图添加到当前的 VC 时,您必须首先将该视图控制器作为子项添加到当前的视图控制器中。这样 UIKit 将正确地向子视图控制器提供所有触摸以及其他与 VC 相关的事件:

DispatchQueue.main.async {
    self.addChildViewController(manualTag)
    self.view.addSubview(manualTag.view)
    manualTag.didMove(toParentViewController: self)

    UIView.animate(withDuration: 0.5, animations: {
        manualTag.view.transform = .identity
    })
}

此外,我不建议您修改 UIViewController 的“视图”属性。这可能会导致不同的问题,例如 UITextField 无响应或 Autolayout 崩溃。只需在 viewDidLoad 方法中将您的地图添加为全屏子视图:

super.viewDidLoad()

let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
mapView.frame = view.bounds
view.addSubview(mapView)

并且不要忘记删除您对 loadView() 方法的覆盖!

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在链接上使用:before和:after而不将内容作为链接的一部分

来自分类Dev

Linq获取数组和求和作为返回数据集的一部分

来自分类Dev

paste0和ifelse作为dplyr中管道的一部分

来自分类Dev

休眠Lucene搜索包括“ the”和“ a”作为搜索的一部分

来自分类Dev

Linq获取数组和求和作为返回数据集的一部分

来自分类Dev

Android - 处理作为解决方案一部分的图像和下载的图像

来自分类Dev

UITextField 和 UIButton 在 signOut segue swift 后无响应

来自分类Dev

将某些内容作为路径的一部分和参数作为参数是否等效?

来自分类Dev

如何编写正确的zipfile校验和值,并将其作为zipfile内容的一部分

来自分类Dev

如何在Logrotate上使用日期和时间作为文件名的一部分?

来自分类Dev

哪个“ C ++可再发行文件”作为.NET Framework 3.5和4的一部分安装?

来自分类Dev

在GooGle地图上添加UITextField和UiButton

来自分类Dev

在Java中将JSON响应作为Rest调用的一部分

来自分类Dev

如何在Flask中返回图像作为GET请求响应的一部分?

来自分类Dev

作为AJAX成功函数的一部分,从API调用发送PHP的JSON响应

来自分类Dev

cURL - 使用特定的响应头作为文件名的一部分

来自分类Dev

使用响应头作为 Retrofit 返回对象的一部分

来自分类Dev

作为XMLHttpRequest和FormData的一部分发送多个文件

来自分类Dev

如何添加和/或作为字符串的一部分进行查询?

来自分类Dev

有没有一种方法可以停止将Web模式的Web Storage和IndexedDB作为源的一部分?

来自分类Dev

作为表格一部分的单选按钮

来自分类Dev

UiSegmentedControl作为UILabel子类的一部分

来自分类Dev

将标题作为CURL的一部分

来自分类Dev

块作为块的一部分

来自分类Dev

Promise 作为 redux 状态的一部分

来自分类Dev

作为.NET可执行文件中托管模块的一部分,PE标头和CLR标头中包含什么?

来自分类Dev

作为.NET可执行文件中托管模块的一部分,PE标头和CLR标头中包含什么?

来自分类Dev

日期没有破折号和冒号ms访问+日期可以作为自动编号的一部分吗?

来自分类Dev

在UITextField中添加一个UIButton

Related 相关文章

  1. 1

    在链接上使用:before和:after而不将内容作为链接的一部分

  2. 2

    Linq获取数组和求和作为返回数据集的一部分

  3. 3

    paste0和ifelse作为dplyr中管道的一部分

  4. 4

    休眠Lucene搜索包括“ the”和“ a”作为搜索的一部分

  5. 5

    Linq获取数组和求和作为返回数据集的一部分

  6. 6

    Android - 处理作为解决方案一部分的图像和下载的图像

  7. 7

    UITextField 和 UIButton 在 signOut segue swift 后无响应

  8. 8

    将某些内容作为路径的一部分和参数作为参数是否等效?

  9. 9

    如何编写正确的zipfile校验和值,并将其作为zipfile内容的一部分

  10. 10

    如何在Logrotate上使用日期和时间作为文件名的一部分?

  11. 11

    哪个“ C ++可再发行文件”作为.NET Framework 3.5和4的一部分安装?

  12. 12

    在GooGle地图上添加UITextField和UiButton

  13. 13

    在Java中将JSON响应作为Rest调用的一部分

  14. 14

    如何在Flask中返回图像作为GET请求响应的一部分?

  15. 15

    作为AJAX成功函数的一部分,从API调用发送PHP的JSON响应

  16. 16

    cURL - 使用特定的响应头作为文件名的一部分

  17. 17

    使用响应头作为 Retrofit 返回对象的一部分

  18. 18

    作为XMLHttpRequest和FormData的一部分发送多个文件

  19. 19

    如何添加和/或作为字符串的一部分进行查询?

  20. 20

    有没有一种方法可以停止将Web模式的Web Storage和IndexedDB作为源的一部分?

  21. 21

    作为表格一部分的单选按钮

  22. 22

    UiSegmentedControl作为UILabel子类的一部分

  23. 23

    将标题作为CURL的一部分

  24. 24

    块作为块的一部分

  25. 25

    Promise 作为 redux 状态的一部分

  26. 26

    作为.NET可执行文件中托管模块的一部分,PE标头和CLR标头中包含什么?

  27. 27

    作为.NET可执行文件中托管模块的一部分,PE标头和CLR标头中包含什么?

  28. 28

    日期没有破折号和冒号ms访问+日期可以作为自动编号的一部分吗?

  29. 29

    在UITextField中添加一个UIButton

热门标签

归档