2017-12-02 4 views
0

の一部として応答しない、私はこのコードを持って、それがUITextFieldUIButtonためUIViewControllerです:はUIButtonで、UITextFieldが、私は迅速に新しいですのUIViewController

私のメイン UIViewController
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 
    } 
} 

- それがロードされますこのように、ボタンをクリックします。それは細かいロードされます - 私はちょうど私がここに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 
      }) 
     } 
    } 

問題がUIButtonUITextFieldは動作しません...どちらも、それらをタップに反応するもされていませんとにかく私は間違って何をしていますか?ちょうど包みViewControllerのすべての投稿

UPDATE

、私のメインビューコントローラ、:あなたは、現在のVCにいくつかの他のView Controllerのビューを追加すると

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() 
    } 
} 
+0

isUirのisUserInteractionEnabledプロパティが両方ともtrueに設定されていますか? – Xcoder

+0

ありがとうございましたが、それは助けになりませんでした – ewizard

+0

開いていると、マニュアルタグの視界の外で私のタップに画面が応答します – ewizard

答えて

1

はあなたがまずそれを追加する必要があります現在のコントローラの子として表示コントローラ。 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メソッド()メソッドのオーバーライドを削除することを忘れないでください!

+0

ありがとう!それはmanualTagのボタンを解決します....しかし、UITextFieldは依然として – ewizard

+0

に応答していませんので、UITextFieldデリゲートメソッドは呼び出されていません。 –

+0

キーボードが決して表示されない...病気にログする – ewizard

関連する問題