2016-07-02 202 views
0

當UITextField添加到ContainerView時,它對觸摸事件沒有響應,我有一個問題。當我嘗試將UITextField添加到主UIViewController時,它似乎很好,它響應(鍵盤顯示)。當添加到UIView時,UITextField對觸摸事件沒有響應

我試圖將子視圖放到前面,在ContainerView上啓用用戶交互 - 沒有任何幫助。

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    self.scrollView = UIScrollView() 
    self.containerView = UIView() 

    self.scrollView.delegate = self 
    self.containerView.userInteractionEnabled = true 

    self.scrollView.addSubview(containerView) 
    self.view.addSubview(scrollView) 

    registerForKeyboardNotifications() 
    setupGesturesAndMap() 
    setupActivityIndicator() 
    setupCircleButton() 
    setupBrowseTextField() 

    // Initializing status 
    status = Status.Init 
} 

override func viewDidLayoutSubviews() { 
    super.viewDidLayoutSubviews() 

    scrollView.frame = self.view.bounds 
    containerView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height) 
} 

,並有功能負責browseTextField的設置:

func setupBrowseTextField() { 

    // Setup of browseTextField 
    browseTextField = UITextField(frame: CGRectMake(10, self.view.frame.height/2, self.view.frame.width - 20, self.view.frame.height/12)) 
    browseTextField.placeholder = "Enter your destination here" 
    browseTextField.font = UIFont.systemFontOfSize(15) 
    browseTextField.borderStyle = UITextBorderStyle.RoundedRect 
    browseTextField.autocorrectionType = UITextAutocorrectionType.No 
    browseTextField.keyboardType = UIKeyboardType.Default 
    browseTextField.returnKeyType = UIReturnKeyType.Done 
    browseTextField.clearButtonMode = UITextFieldViewMode.WhileEditing 
    browseTextField.contentVerticalAlignment = UIControlContentVerticalAlignment.Center 
    browseTextField.delegate = self 

    containerView.addSubview(browseTextField) 
} 

會有人對你有什麼建議?

+0

不,沒有幫助 – koncek

+0

我認爲你必須將文本框的委託更改爲滾動型或容器視圖。所以:'browseTextField.delegate = self.scrollView' **或**'browseTextField.delegate = self.containerView' – Eric

+0

我不認爲將委託設置爲滾動視圖或容器視圖會影響任何東西,說實話(爲什麼會這樣?)。很可能你有一些手勢與文本字段點擊衝突。我建議你在手勢委託中添加'-gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:'方法,看看它是否有效。 –

回答

0

我看你已經設置了containerView的幀

containerView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height) 

scrollView.contentSize.width爲0

這可以解決你的問題。

containerView.frame = self.view.bounds 

而且你可以給通過滾動視圖的幀大小containerView大小

containerView.frame = CGRectMake(0, 0, scrollView.frame.width, scrollView.frame.height) 
+0

非常感謝,scrollView.contentSize.width其實是0 ....我永遠不會想到;)再次感謝。 – koncek